Index: trunk/src/org/expeditee/items/Item.java
===================================================================
--- trunk/src/org/expeditee/items/Item.java	(revision 1505)
+++ trunk/src/org/expeditee/items/Item.java	(revision 1509)
@@ -49,5 +49,7 @@
 import org.expeditee.core.bounds.PolygonBounds;
 import org.expeditee.encryption.core.EncryptedImage;
+import org.expeditee.encryption.io.EncryptedExpWriter;
 import org.expeditee.encryption.items.KeyType;
+import org.expeditee.encryption.items.UserAppliedEncryptionPermission;
 import org.expeditee.encryption.items.surrogates.EncryptionDetail;
 import org.expeditee.encryption.items.surrogates.EncryptionDetail.Type;
@@ -4097,21 +4099,221 @@
 		}
 	}
-	
+		
 	public void setEncryptionLabel(String label) {
-		this.getSurrogates().clear();
-		this._encryptionLabel = label;
-		
+		if (label == null) {
+			_encryptionLabel = null;
+			return;
+		}
+				
 		if (this.getParent().getFrameEncryptionLabel() == null) {
 			MessageBay.displayMessage("Items can only be entrypted if the frame they are on is.");
 			return;
 		}
+				
 		LabelInfo labelResult = Label.getLabel(label);
 		if (!labelResult.is(LabelResult.SuccessResolveLabelToKey)) {
 			MessageBay.displayMessage(labelResult.toString());
-			this._encryptionLabel = null;
 			return;
 		}
 		
+		String errorMessage = "You do not have sufficient permissions to set the encryption label to " + label;
+		UserAppliedEncryptionPermission p = getParent().getEncryptionPermission().getPermission(getParent().getOwner(), getParent().getGroupMembers());
+		String homogeneousEncryptionLabel = getParent().getHomogeneousEncryptionLabel();
+		if (homogeneousEncryptionLabel == null) { homogeneousEncryptionLabel = getParent().getFrameEncryptionLabel(); }
+		if (homogeneousEncryptionLabel == null) { homogeneousEncryptionLabel = EncryptedExpWriter.getLabelNone(); } 		
+		List<String> hetrogeneousFrameOwnerLabels = getParent().getHetrogeneousFrameOwnerLabels();
+		
+		switch(p) {
+		case none:
+			MessageBay.errorMessage(errorMessage);
+			return;
+		case homogeneous:
+			if (!homogeneousEncryptionLabel.equals(label)) {
+				MessageBay.errorMessage(errorMessage);
+				return;
+			}
+			break;
+		case hetrogeneous_owner:
+			if (!homogeneousEncryptionLabel.equals(label) && !hetrogeneousFrameOwnerLabels.contains(label)) {
+				MessageBay.errorMessage(errorMessage);
+				return;
+			}
+			break;
+		case hetrogeneous: break;
+		}
+		
+		this.getSurrogates().clear();
+		this._encryptionLabel = label;
+		
 		//Setup surrogate items.
+		generateSurrogate(labelResult);
+	}
+	
+	public void setEncryptionLabelOnLoad(String label) {
+		this._encryptionLabel = label;
+	}
+
+	/**
+	 * Replaces the given dot item with a text item displaying the given character.
+	 * 
+	 * @param dot
+	 * 		The Dot item to replace.
+	 * 
+	 * @param ch
+	 * 		The character to display as the text for the created Text item.
+	 * 
+	 * @return
+	 * 		The created Text item.
+	 */
+	public static Text replaceDot(Item dot, char ch) {
+		// TODO: Should this make sure 'dot' is actually a Dot and not some other item? cts16
+		Text text = Text.createText(ch);
+		Item.DuplicateItem(dot, text);
+		FrameUtils.setLastEdited(text);
+
+		// Copy the lines list so it can be modified
+		List<Line> lines = new LinkedList<Line>(dot.getLines());
+		for (Line line : lines)
+			line.replaceLineEnd(dot, text);
+		Frame current = dot.getParentOrCurrentFrame();
+		current.removeItem(dot);
+		ItemUtils.EnclosedCheck(current.getSortedItems());
+		return text;
+		// TODO: Should this add the newly-created Text item to the frame? cts16
+	}
+
+	/**
+	 * Replaces the given text item with a dot
+	 */
+	public static Item replaceText(Item text) {
+		Item dot = new Dot(text.getX(), text.getY(), text.getID());
+		Item.DuplicateItem(text, dot);
+
+		List<Line> lines = new LinkedList<Line>();
+		lines.addAll(text.getLines());
+		if (lines.size() > 0)
+			dot.setColor(lines.get(0).getColor());
+		for (Line line : lines) {
+			line.replaceLineEnd(text, dot);
+		}
+		text.delete();
+		Frame current = text.getParentOrCurrentFrame();
+		current.addItem(dot);
+		DisplayController.setCursor(Item.DEFAULT_CURSOR);
+		ItemUtils.EnclosedCheck(current.getSortedItems());
+		return dot;
+	}
+
+	public Set<Item> getSurrogates() {
+		return surrogateItems;
+	}
+		
+	private void setAsSurrogateFor(Item primary) {
+		this.surrogateFor = primary;
+	}
+
+	public void addToSurrogates(Item surrogate) {
+		if (this.getEncryptionLabel() == null) {
+			MessageBay.displayMessage("Only Items with encryption labels can have surrogates.");
+			return;
+		}
+		
+		this.getParent().removeItem(surrogate);
+		this.surrogateItems.add(surrogate);
+		surrogate.setAsSurrogateFor(this);
+		this.getParent().addItem(surrogate);
+		
+		EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave);
+		EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
+		
+		for (Character tag: DefaultFrameWriter.getItemCharTags().keySet()) {
+			if (tag == 'T') {
+				primaryPropertyEncryption.put(tag + "", reencryptOnSave.clone());
+			} else {
+				primaryPropertyEncryption.put(tag + "", unencryptedOnSave.clone());
+			}
+					
+			if (tag == 'T' || tag == 'S') {
+				surrogate.surrogatePropertyInheritance.put(tag + "", false);
+			} else {
+				surrogate.surrogatePropertyInheritance.put(tag + "", true);
+			}
+		}
+		
+		for (String tag: DefaultFrameWriter.getItemStrTags().keySet()) {
+			primaryPropertyEncryption.put(tag, unencryptedOnSave.clone());
+			surrogate.surrogatePropertyInheritance.put(tag + "", true);
+		}
+	}
+	
+	public boolean isSurrogate() {
+		return this.surrogateFor != null;
+	}
+	
+	public Item getPrimary() {
+		return this.surrogateFor;
+	}
+	
+	public EncryptionDetail getEncryptionDetailForTag(String tag) {
+		EncryptionDetail encryptionDetail = primaryPropertyEncryption.get(tag);
+		
+		if (encryptionDetail == null) {
+			EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
+			return unencryptedOnSave;
+		} else {
+			return encryptionDetail;
+		}
+	}
+	
+	public void setEncryptionDetailForTag(String tag, EncryptionDetail encryptionDetail) {
+		primaryPropertyEncryption.put(tag, encryptionDetail);
+	}
+	
+	public boolean isTagInherited(String tag) {
+		return surrogatePropertyInheritance.get(tag);
+	}
+	
+	public void setTagNotInherited(String tag) {
+		surrogatePropertyInheritance.put(tag, false);
+	}
+	
+	public boolean hasAccessToItemAsPrimary() {
+		String label = this.getEncryptionLabel();
+		if (this.isSurrogate() || label == null || label.length() == 0) {
+			return true;
+		}
+		
+		return Label.getLabel(label).is(LabelResult.SuccessResolveLabelToKey);
+	}
+	
+
+	/**
+	 * If when setting a property, we find that the primary has a undeciphered value, when we must continue using that undeciphered value
+	 * when eventually saving.
+	 * @return
+	 */
+	protected boolean subjectToInheritanceCheckOnSave(String tag) {
+		if (tag.equals(DefaultFrameWriter.TYPE_AND_ID_STR)) {
+			return false;
+		}
+		Item primary = getPrimary();
+		if (primary == null) return false;
+		EncryptionDetail primaryItemTagEncryptionDetail = primary.primaryPropertyEncryption.get(tag);
+		Type primaryItemTagEncryptionDetailType = primaryItemTagEncryptionDetail.getEncryptionDetailType();
+		return primaryItemTagEncryptionDetailType != EncryptionDetail.Type.UseUndecipheredValueOnSave;
+	}
+
+	public boolean acceptsKeyboardEnter() {
+		return _acceptsKeyboardEnter;
+	}
+		
+	public void setAcceptsEnter(boolean value) {
+		if (this.getText() != null && this.getText().equals("Beep")) {
+			System.err.println();
+		}
+		_acceptsKeyboardEnter = value;
+	}
+	
+	private void generateSurrogate(LabelInfo labelResult) {
 		Item copy = this.copy();
 		if (copy.isAnnotation()) {
@@ -4155,168 +4357,3 @@
 		this.addToSurrogates(copy);
 	}
-	
-	public void setEncryptionLabelOnLoad(String label) {
-		this._encryptionLabel = label;
-	}
-
-	/**
-	 * Replaces the given dot item with a text item displaying the given character.
-	 * 
-	 * @param dot
-	 * 		The Dot item to replace.
-	 * 
-	 * @param ch
-	 * 		The character to display as the text for the created Text item.
-	 * 
-	 * @return
-	 * 		The created Text item.
-	 */
-	public static Text replaceDot(Item dot, char ch) {
-		// TODO: Should this make sure 'dot' is actually a Dot and not some other item? cts16
-		Text text = Text.createText(ch);
-		Item.DuplicateItem(dot, text);
-		FrameUtils.setLastEdited(text);
-
-		// Copy the lines list so it can be modified
-		List<Line> lines = new LinkedList<Line>(dot.getLines());
-		for (Line line : lines)
-			line.replaceLineEnd(dot, text);
-		Frame current = dot.getParentOrCurrentFrame();
-		current.removeItem(dot);
-		ItemUtils.EnclosedCheck(current.getSortedItems());
-		return text;
-		// TODO: Should this add the newly-created Text item to the frame? cts16
-	}
-
-	/**
-	 * Replaces the given text item with a dot
-	 */
-	public static Item replaceText(Item text) {
-		Item dot = new Dot(text.getX(), text.getY(), text.getID());
-		Item.DuplicateItem(text, dot);
-
-		List<Line> lines = new LinkedList<Line>();
-		lines.addAll(text.getLines());
-		if (lines.size() > 0)
-			dot.setColor(lines.get(0).getColor());
-		for (Line line : lines) {
-			line.replaceLineEnd(text, dot);
-		}
-		text.delete();
-		Frame current = text.getParentOrCurrentFrame();
-		current.addItem(dot);
-		DisplayController.setCursor(Item.DEFAULT_CURSOR);
-		ItemUtils.EnclosedCheck(current.getSortedItems());
-		return dot;
-	}
-
-	public Set<Item> getSurrogates() {
-		return surrogateItems;
-	}
-		
-	private void setAsSurrogateFor(Item primary) {
-		this.surrogateFor = primary;
-	}
-
-	public void addToSurrogates(Item surrogate) {
-		if (this.getEncryptionLabel() == null) {
-			MessageBay.displayMessage("Only Items with encryption labels can have surrogates.");
-			return;
-		}
-		
-		this.getParent().removeItem(surrogate);
-		this.surrogateItems.add(surrogate);
-		surrogate.setAsSurrogateFor(this);
-		this.getParent().addItem(surrogate);
-		
-		EncryptionDetail reencryptOnSave = new EncryptionDetail(EncryptionDetail.Type.ReencryptOnSave);
-		EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
-		
-		for (Character tag: DefaultFrameWriter.getItemCharTags().keySet()) {
-			if (tag == 'T') {
-				primaryPropertyEncryption.put(tag + "", reencryptOnSave.clone());
-			} else {
-				primaryPropertyEncryption.put(tag + "", unencryptedOnSave.clone());
-			}
-					
-			if (tag == 'T' || tag == 'S') {
-				surrogate.surrogatePropertyInheritance.put(tag + "", false);
-			} else {
-				surrogate.surrogatePropertyInheritance.put(tag + "", true);
-			}
-		}
-		
-		for (String tag: DefaultFrameWriter.getItemStrTags().keySet()) {
-			primaryPropertyEncryption.put(tag, unencryptedOnSave.clone());
-			surrogate.surrogatePropertyInheritance.put(tag + "", true);
-		}
-	}
-	
-	public boolean isSurrogate() {
-		return this.surrogateFor != null;
-	}
-	
-	public Item getPrimary() {
-		return this.surrogateFor;
-	}
-	
-	public EncryptionDetail getEncryptionDetailForTag(String tag) {
-		EncryptionDetail encryptionDetail = primaryPropertyEncryption.get(tag);
-		
-		if (encryptionDetail == null) {
-			EncryptionDetail unencryptedOnSave = new EncryptionDetail(EncryptionDetail.Type.UnencryptedOnSave);
-			return unencryptedOnSave;
-		} else {
-			return encryptionDetail;
-		}
-	}
-	
-	public void setEncryptionDetailForTag(String tag, EncryptionDetail encryptionDetail) {
-		primaryPropertyEncryption.put(tag, encryptionDetail);
-	}
-	
-	public boolean isTagInherited(String tag) {
-		return surrogatePropertyInheritance.get(tag);
-	}
-	
-	public void setTagNotInherited(String tag) {
-		surrogatePropertyInheritance.put(tag, false);
-	}
-	
-	public boolean hasAccessToItemAsPrimary() {
-		String label = this.getEncryptionLabel();
-		if (this.isSurrogate() || label == null || label.length() == 0) {
-			return true;
-		}
-		
-		return Label.getLabel(label).is(LabelResult.SuccessResolveLabelToKey);
-	}
-	
-
-	/**
-	 * If when setting a property, we find that the primary has a undeciphered value, when we must continue using that undeciphered value
-	 * when eventually saving.
-	 * @return
-	 */
-	protected boolean subjectToInheritanceCheckOnSave(String tag) {
-		if (tag.equals(DefaultFrameWriter.TYPE_AND_ID_STR)) {
-			return false;
-		}
-		Item primary = getPrimary();
-		if (primary == null) return false;
-		EncryptionDetail primaryItemTagEncryptionDetail = primary.primaryPropertyEncryption.get(tag);
-		Type primaryItemTagEncryptionDetailType = primaryItemTagEncryptionDetail.getEncryptionDetailType();
-		return primaryItemTagEncryptionDetailType != EncryptionDetail.Type.UseUndecipheredValueOnSave;
-	}
-
-	public boolean acceptsKeyboardEnter() {
-		return _acceptsKeyboardEnter;
-	}
-		
-	public void setAcceptsEnter(boolean value) {
-		if (this.getText() != null && this.getText().equals("Beep")) {
-			System.err.println();
-		}
-		_acceptsKeyboardEnter = value;
-	}
 }
