001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.him.metadata.swing;
021:
022: import java.awt.*;
023:
024: import javax.swing.*;
025: import javax.swing.border.*;
026:
027: import org.openharmonise.commons.xml.namespace.*;
028: import org.openharmonise.him.metadata.range.swing.*;
029: import org.openharmonise.vfs.metadata.*;
030:
031: /**
032: * @author Matthew Large
033: *
034: */
035: public class PropertyPanel extends JPanel implements LayoutManager {
036:
037: private PropertyInstance m_propertyInstance = null;
038: private String m_sName = null;
039:
040: private boolean m_bResizeWidth = true;
041:
042: private HelpIcon m_helpIcon = null;
043:
044: private JLabel m_titleLabel = null;
045:
046: /**
047: *
048: */
049: public PropertyPanel(PropertyInstance propertyInstance,
050: boolean bResizeWidth) {
051: super ();
052: this .m_propertyInstance = propertyInstance;
053: this .m_bResizeWidth = bResizeWidth;
054: this .setup();
055: }
056:
057: /**
058: *
059: */
060: public PropertyPanel(String sName, boolean bResizeWidth) {
061: super ();
062: this .m_sName = sName;
063: this .m_bResizeWidth = bResizeWidth;
064: this .setup();
065: }
066:
067: public Property getProperty() {
068: Property prop = null;
069:
070: if (this .m_propertyInstance != null
071: && this .m_propertyInstance.getDefinition() != null) {
072: prop = this .m_propertyInstance.getDefinition();
073: }
074:
075: return prop;
076: }
077:
078: /**
079: * @param arg0
080: */
081: private PropertyPanel(boolean arg0) {
082: super (arg0);
083: }
084:
085: /**
086: * @param arg0
087: */
088: private PropertyPanel(LayoutManager arg0) {
089: super (arg0);
090: }
091:
092: /**
093: * @param arg0
094: * @param arg1
095: */
096: private PropertyPanel(LayoutManager arg0, boolean arg1) {
097: super (arg0, arg1);
098: }
099:
100: private void setup() {
101: this .setLayout(this );
102:
103: TitledBorder border = null;
104:
105: if (this .m_propertyInstance != null) {
106: if (this .m_propertyInstance.getNamespaceURI().equals(
107: NamespaceType.OHRM.getURI())
108: && this .m_propertyInstance.getName()
109: .equals("title")) {
110: this .m_titleLabel = new JLabel("Title");
111: } else {
112: this .m_titleLabel = new JLabel(this .m_propertyInstance
113: .getDefinition().getDisplayName());
114: }
115: } else if (this .m_sName != null) {
116: this .m_titleLabel = new JLabel(this .m_sName);
117: }
118: String fontName = "Dialog";
119: int fontSize = 13;
120: Font font = new Font(fontName, Font.BOLD, fontSize);
121: this .m_titleLabel.setFont(font);
122: this .add(m_titleLabel);
123:
124: if (this .m_propertyInstance != null
125: && this .m_propertyInstance.getDefinition() != null) {
126: if (this .m_propertyInstance.getName().equals("domain")) {
127: this .m_helpIcon = new HelpIcon(
128: "Domain",
129: "Domain is used to restrict a Property's use (and occurrence) to a particular set of Collections or Resources, for example you could restrict the Property \"Location\" to the Resources within the Collection \"Case Study\" and restrict the number of locations to \"one\". This would mean that when tagging a Case Study document the vocabularies within Location (e.g. South East, Midlands etc.) would be available to the user and that they need to define one Location.");
130: this .add(m_helpIcon);
131: } else if (this .m_propertyInstance.getName()
132: .equals("range")) {
133: this .m_helpIcon = new HelpIcon("Range",
134: "The Range of a Property is description of its allowable Values.");
135: this .add(m_helpIcon);
136: } else {
137: this .m_helpIcon = new HelpIcon(this .m_propertyInstance
138: .getDefinition());
139: this .add(m_helpIcon);
140: }
141: }
142: }
143:
144: /* (non-Javadoc)
145: * @see java.awt.Component#getPreferredSize()
146: */
147: public Dimension getPreferredSize() {
148: int nHeight = 0;
149: int nCount = this .getComponentCount();
150: int nWidth = 0;
151: for (int i = 0; i < nCount; i++) {
152: Component comp = this .getComponent(i);
153: int nDepth = comp.getLocation().y
154: + comp.getPreferredSize().height;
155: int nCurrWidth = comp.getLocation().x
156: + comp.getPreferredSize().width;
157: if (nDepth > nHeight) {
158: nHeight = nDepth;
159: }
160: if (nCurrWidth > nWidth) {
161: nWidth = nCurrWidth;
162: }
163: }
164:
165: if (this .m_bResizeWidth) {
166: return new Dimension(this .getParent().getWidth() - 20,
167: nHeight + 5);
168: } else {
169: return new Dimension(nWidth, nHeight + 5);
170: }
171: }
172:
173: /* (non-Javadoc)
174: * @see java.awt.Component#setEnabled(boolean)
175: */
176: public void setEnabled(boolean bEnabled) {
177: for (int i = 0; i < this .getComponentCount(); i++) {
178: Component comp = this .getComponent(i);
179: comp.setEnabled(bEnabled);
180: }
181: super .setEnabled(bEnabled);
182: }
183:
184: public AbstractRangeDisplay getRangeDisplay() {
185: for (int i = 0; i < this .getComponentCount(); i++) {
186: Component comp = this .getComponent(i);
187: if (comp instanceof AbstractRangeDisplay) {
188: return (AbstractRangeDisplay) comp;
189: }
190: }
191: return null;
192: }
193:
194: /* (non-Javadoc)
195: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
196: */
197: public void layoutContainer(Container arg0) {
198: int nHeight = 35;
199: for (int i = 0; i < this .getComponentCount(); i++) {
200: Component comp = this .getComponent(i);
201: if (comp instanceof AbstractRangeDisplay) {
202: comp.setSize(comp.getPreferredSize());
203: comp.setLocation(5, nHeight);
204: } else if (comp instanceof HelpIcon) {
205: comp.setSize(20, 20);
206: comp
207: .setLocation(this .m_titleLabel
208: .getPreferredSize().width + 15, 10);
209: } else if (comp == this .m_titleLabel) {
210: comp.setSize(
211: this .m_titleLabel.getPreferredSize().width, 30);
212: comp.setLocation(5, 5);
213: }
214: }
215: }
216:
217: /* (non-Javadoc)
218: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
219: */
220: public void removeLayoutComponent(Component arg0) {
221: }
222:
223: /* (non-Javadoc)
224: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
225: */
226: public void addLayoutComponent(String arg0, Component arg1) {
227: }
228:
229: /* (non-Javadoc)
230: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
231: */
232: public Dimension minimumLayoutSize(Container arg0) {
233: return this .getPreferredSize();
234: }
235:
236: /* (non-Javadoc)
237: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
238: */
239: public Dimension preferredLayoutSize(Container arg0) {
240: return this.getPreferredSize();
241: }
242: }
|