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: package org.openharmonise.him.metadata.range.swing.resourcehandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.him.*;
029: import org.openharmonise.swing.FontManager;
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.gui.*;
032: import org.openharmonise.vfs.metadata.value.*;
033: import org.openharmonise.vfs.servers.ServerList;
034:
035: /**
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.2 $
039: *
040: */
041: public class ResourceSelectionCell extends JPanel implements
042: LayoutManager, ActionListener, MouseListener {
043:
044: private Reason m_reason = null;
045:
046: private Color m_selectedColor = new Color(173, 169, 143);
047:
048: private String m_sFilePath = null;
049: private AbstractVirtualFileSystem m_vfs = ServerList.getInstance()
050: .getHarmoniseServer().getVFS();
051:
052: private ResourceSelectionList m_list = null;
053:
054: private JLabel m_resourceLabel = null;
055: private JLabel m_errorLabel = null;
056: private JComboBox m_reasonComboBox = null;
057:
058: private int m_nWidth = 270;
059:
060: private boolean m_bInSetup = true;
061:
062: /**
063: * @param list
064: * @param vfFile
065: */
066: public ResourceSelectionCell(ResourceSelectionList list,
067: VirtualFile vfFile) {
068: super ();
069: this .m_list = list;
070: this .m_sFilePath = vfFile.getFullPath();
071: this .setup();
072: }
073:
074: /**
075: * @param value
076: * @param reason
077: * @param list
078: */
079: public ResourceSelectionCell(ResourceValue value, Reason reason,
080: ResourceSelectionList list) {
081: super ();
082: this .m_list = list;
083: this .m_sFilePath = value.getValue();
084: this .m_reason = reason;
085: this .setup();
086: }
087:
088: private void setup() {
089: this .setLayout(this );
090: this .setBackground(Color.WHITE);
091:
092: this .m_errorLabel = new JLabel();
093: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
094: "16-message-error.gif"));
095: this .m_errorLabel.addMouseListener(this );
096: this .add(this .m_errorLabel);
097:
098: VirtualFile vfFile = this .m_vfs.getVirtualFile(m_sFilePath)
099: .getResource();
100: this .m_resourceLabel = new JLabel(this .m_vfs
101: .getVirtualFileSystemView().getDisplayName(vfFile));
102: this .m_resourceLabel.setToolTipText(this .m_vfs
103: .getVirtualFileSystemView().getDisplayName(vfFile));
104: this .m_resourceLabel.setIcon(this .m_vfs
105: .getVirtualFileSystemView().getIcon(vfFile));
106: this .m_resourceLabel.setForeground(Color.RED);
107: this .m_resourceLabel.addMouseListener(this );
108: this .m_resourceLabel.setFont(FontManager.getInstance().getFont(
109: FontManager.FONT_RESOURCE_TITLE));
110: this .add(this .m_resourceLabel);
111:
112: this .refreshReasons();
113: this .add(this .m_reasonComboBox);
114: this .checkValid();
115: this .m_bInSetup = false;
116: }
117:
118: private void checkValid() {
119: if (this .m_reason != null && this .m_reason.isValid()) {
120: this .m_errorLabel.setIcon(IconManager.getInstance()
121: .getIcon("16-message-confirm.gif"));
122: this .m_resourceLabel.setForeground(Color.BLACK);
123: } else {
124: this .m_errorLabel.setIcon(IconManager.getInstance()
125: .getIcon("16-message-error.gif"));
126: this .m_resourceLabel.setForeground(Color.RED);
127: }
128: }
129:
130: protected boolean isSelected() {
131: if (this .getBackground() == this .m_selectedColor) {
132: return true;
133: } else {
134: return false;
135: }
136: }
137:
138: protected void prepareToRemove() {
139: if (this .m_reason != null) {
140: this .m_reason.removeValue(m_sFilePath);
141: }
142: }
143:
144: protected void setCellSelected(boolean bSelected) {
145: if (!bSelected) {
146: this .setBackground(Color.WHITE);
147: this .m_resourceLabel.setBackground(Color.WHITE);
148: this .m_errorLabel.setBackground(Color.WHITE);
149: this .m_reasonComboBox.setBackground(Color.WHITE);
150: } else {
151: this .setBackground(this .m_selectedColor);
152: this .m_resourceLabel.setBackground(this .m_selectedColor);
153: this .m_errorLabel.setBackground(this .m_selectedColor);
154: this .m_reasonComboBox.setBackground(this .m_selectedColor);
155: }
156: this .validateTree();
157: }
158:
159: private Reason getReasonByName(String sReasonName) {
160: List reasons = this .m_list.getReasons();
161: Iterator itor = reasons.iterator();
162: while (itor.hasNext()) {
163: Reason tempReason = (Reason) itor.next();
164: if (tempReason.getDisplayName().equals(sReasonName)) {
165: return tempReason;
166: }
167: }
168: return null;
169: }
170:
171: protected void setReason(Reason newReason) {
172: if (this .m_reason != null
173: && newReason != null
174: && this .m_reason != newReason
175: && !this .m_reason.getDisplayName().equalsIgnoreCase(
176: newReason.getDisplayName())) {
177: this .m_reason.removeValue(this .m_sFilePath);
178: }
179: if (newReason != null
180: && (this .m_reason == null || this .m_reason != newReason)
181: && (this .m_reason == null || !this .m_reason
182: .getDisplayName().equalsIgnoreCase(
183: newReason.getDisplayName()))) {
184: newReason.addValue(this .m_sFilePath);
185: this .m_reason = newReason;
186: }
187: }
188:
189: /* (non-Javadoc)
190: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
191: */
192: public void actionPerformed(ActionEvent ae) {
193: if (ae.getActionCommand().equals("COMBO")) {
194: Reason newReason = null;
195: String sReasonName = (String) this .m_reasonComboBox
196: .getSelectedItem();
197:
198: newReason = this .getReasonByName(sReasonName);
199: if (newReason != null) {
200: this .setReason(newReason);
201: this .m_reasonComboBox.setSelectedItem(this .m_reason
202: .getDisplayName());
203: } else {
204: if (this .m_reason != null) {
205: this .m_reasonComboBox.setSelectedItem(this .m_reason
206: .getDisplayName());
207: }
208: }
209: this .checkValid();
210: }
211: }
212:
213: public void refreshReasons() {
214: List aUseableReasons = new ArrayList();
215: aUseableReasons.add("");
216: List reasons = this .m_list.getReasons();
217: for (int i = 0; i < reasons.size(); i++) {
218: Reason reason = (Reason) reasons.get(i);
219: if (reason.isValidReasonForResource(m_vfs, m_sFilePath)) {
220: aUseableReasons.add(reason.getDisplayName());
221: }
222: }
223: String[] aReasons = new String[aUseableReasons.size()];
224: for (int i = 0; i < aUseableReasons.size(); i++) {
225: aReasons[i] = (String) aUseableReasons.get(i);
226: }
227:
228: String sSelectedReason = "";
229: if (this .m_reasonComboBox == null) {
230: if (this .m_reason == null && aReasons.length == 2) {
231: sSelectedReason = aReasons[1];
232:
233: } else if (this .m_reason != null) {
234: sSelectedReason = this .m_reason.getDisplayName();
235: }
236: this .m_reasonComboBox = this .getCombo(sSelectedReason,
237: aReasons);
238: } else {
239: this .remove(this .m_reasonComboBox);
240: sSelectedReason = (String) this .m_reasonComboBox
241: .getSelectedItem();
242: if (this .m_reason == null && sSelectedReason.equals("")
243: && aReasons.length == 2) {
244: sSelectedReason = aReasons[1];
245: } else if (this .m_reason != null) {
246: sSelectedReason = this .m_reason.getDisplayName();
247: }
248: this .m_reasonComboBox = this .getCombo(sSelectedReason,
249: aReasons);
250: }
251: this .setReason(this .getReasonByName(sSelectedReason));
252: this .add(m_reasonComboBox);
253: this .checkValid();
254: this .revalidate();
255: }
256:
257: /* (non-Javadoc)
258: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
259: */
260: public void layoutContainer(Container arg0) {
261: this .m_resourceLabel.setSize(m_nWidth - 170, 20);
262: this .m_resourceLabel.setLocation(0, 0);
263: this .m_errorLabel.setSize(20, 20);
264: this .m_errorLabel.setLocation(m_nWidth - 170, 0);
265: this .m_reasonComboBox.setSize(148, 18);
266: this .m_reasonComboBox.setLocation(m_nWidth - 149, 1);
267: }
268:
269: private JComboBox getCombo(String sSelectedItem, String[] aReasons) {
270: JComboBox combo = new JComboBox(aReasons);
271: combo.setSelectedItem(sSelectedItem);
272: combo.addActionListener(this );
273: combo.setActionCommand("COMBO");
274: return combo;
275: }
276:
277: /* (non-Javadoc)
278: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
279: */
280: public void mouseClicked(MouseEvent me) {
281: if (me.getSource() == this .m_resourceLabel
282: || me.getSource() == this .m_errorLabel) {
283: this .m_list.cellSelected(this );
284: }
285: }
286:
287: /* (non-Javadoc)
288: * @see java.awt.Component#getPreferredSize()
289: */
290: public Dimension getPreferredSize() {
291: return new Dimension(m_nWidth, 20);
292: }
293:
294: /* (non-Javadoc)
295: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
296: */
297: public Dimension minimumLayoutSize(Container arg0) {
298: return this .getPreferredSize();
299: }
300:
301: /* (non-Javadoc)
302: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
303: */
304: public Dimension preferredLayoutSize(Container arg0) {
305: return this .getPreferredSize();
306: }
307:
308: /**
309: *
310: */
311: public ResourceSelectionCell() {
312: super ();
313: }
314:
315: /**
316: * @param arg0
317: */
318: private ResourceSelectionCell(boolean arg0) {
319: super (arg0);
320: }
321:
322: /**
323: * @param arg0
324: */
325: private ResourceSelectionCell(LayoutManager arg0) {
326: super (arg0);
327: }
328:
329: /**
330: * @param arg0
331: * @param arg1
332: */
333: private ResourceSelectionCell(LayoutManager arg0, boolean arg1) {
334: super (arg0, arg1);
335: }
336:
337: /* (non-Javadoc)
338: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
339: */
340: public void removeLayoutComponent(Component arg0) {
341: }
342:
343: /* (non-Javadoc)
344: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
345: */
346: public void addLayoutComponent(String arg0, Component arg1) {
347: }
348:
349: /* (non-Javadoc)
350: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
351: */
352: public void mouseEntered(MouseEvent arg0) {
353: }
354:
355: /* (non-Javadoc)
356: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
357: */
358: public void mouseExited(MouseEvent arg0) {
359: }
360:
361: /* (non-Javadoc)
362: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
363: */
364: public void mousePressed(MouseEvent arg0) {
365: }
366:
367: /* (non-Javadoc)
368: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
369: */
370: public void mouseReleased(MouseEvent arg0) {
371: }
372:
373: }
|