1 19 package org.openharmonise.him.metadata.range.swing.resourcehandling; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 28 import org.openharmonise.him.*; 29 import org.openharmonise.swing.FontManager; 30 import org.openharmonise.vfs.*; 31 import org.openharmonise.vfs.gui.*; 32 import org.openharmonise.vfs.metadata.value.*; 33 import org.openharmonise.vfs.servers.ServerList; 34 35 41 public class ResourceSelectionCell extends JPanel implements LayoutManager, ActionListener, MouseListener { 42 43 private Reason m_reason = null; 44 45 private Color m_selectedColor = new Color(173, 169, 143); 46 47 private String m_sFilePath = null; 48 private AbstractVirtualFileSystem m_vfs = ServerList.getInstance().getHarmoniseServer().getVFS(); 49 50 private ResourceSelectionList m_list = null; 51 52 private JLabel m_resourceLabel = null; 53 private JLabel m_errorLabel = null; 54 private JComboBox m_reasonComboBox = null; 55 56 private int m_nWidth = 270; 57 58 private boolean m_bInSetup = true; 59 60 64 public ResourceSelectionCell(ResourceSelectionList list, VirtualFile vfFile) { 65 super(); 66 this.m_list = list; 67 this.m_sFilePath = vfFile.getFullPath(); 68 this.setup(); 69 } 70 71 76 public ResourceSelectionCell(ResourceValue value, Reason reason, ResourceSelectionList list) { 77 super(); 78 this.m_list = list; 79 this.m_sFilePath = value.getValue(); 80 this.m_reason = reason; 81 this.setup(); 82 } 83 84 private void setup() { 85 this.setLayout(this); 86 this.setBackground(Color.WHITE); 87 88 this.m_errorLabel = new JLabel(); 89 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 90 this.m_errorLabel.addMouseListener(this); 91 this.add(this.m_errorLabel); 92 93 VirtualFile vfFile = this.m_vfs.getVirtualFile(m_sFilePath).getResource(); 94 this.m_resourceLabel = new JLabel(this.m_vfs.getVirtualFileSystemView().getDisplayName(vfFile)); 95 this.m_resourceLabel.setToolTipText(this.m_vfs.getVirtualFileSystemView().getDisplayName(vfFile)); 96 this.m_resourceLabel.setIcon( this.m_vfs.getVirtualFileSystemView().getIcon(vfFile) ); 97 this.m_resourceLabel.setForeground(Color.RED); 98 this.m_resourceLabel.addMouseListener(this); 99 this.m_resourceLabel.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) ); 100 this.add(this.m_resourceLabel); 101 102 this.refreshReasons(); 103 this.add(this.m_reasonComboBox); 104 this.checkValid(); 105 this.m_bInSetup = false; 106 } 107 108 private void checkValid() { 109 if(this.m_reason!=null && this.m_reason.isValid()) { 110 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-confirm.gif")); 111 this.m_resourceLabel.setForeground(Color.BLACK); 112 } else { 113 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 114 this.m_resourceLabel.setForeground(Color.RED); 115 } 116 } 117 118 protected boolean isSelected() { 119 if(this.getBackground()==this.m_selectedColor) { 120 return true; 121 } else { 122 return false; 123 } 124 } 125 126 protected void prepareToRemove() { 127 if(this.m_reason!=null) { 128 this.m_reason.removeValue(m_sFilePath); 129 } 130 } 131 132 protected void setCellSelected(boolean bSelected) { 133 if(!bSelected) { 134 this.setBackground(Color.WHITE); 135 this.m_resourceLabel.setBackground(Color.WHITE); 136 this.m_errorLabel.setBackground(Color.WHITE); 137 this.m_reasonComboBox.setBackground(Color.WHITE); 138 } else { 139 this.setBackground(this.m_selectedColor); 140 this.m_resourceLabel.setBackground(this.m_selectedColor); 141 this.m_errorLabel.setBackground(this.m_selectedColor); 142 this.m_reasonComboBox.setBackground(this.m_selectedColor); 143 } 144 this.validateTree(); 145 } 146 147 private Reason getReasonByName(String sReasonName) { 148 List reasons = this.m_list.getReasons(); 149 Iterator itor = reasons.iterator(); 150 while (itor.hasNext()) { 151 Reason tempReason = (Reason) itor.next(); 152 if(tempReason.getDisplayName().equals(sReasonName)) { 153 return tempReason; 154 } 155 } 156 return null; 157 } 158 159 protected void setReason(Reason newReason) { 160 if(this.m_reason!=null && newReason!=null && this.m_reason!=newReason && !this.m_reason.getDisplayName().equalsIgnoreCase(newReason.getDisplayName())) { 161 this.m_reason.removeValue(this.m_sFilePath); 162 } 163 if(newReason!=null && (this.m_reason==null || this.m_reason!=newReason) && (this.m_reason==null || !this.m_reason.getDisplayName().equalsIgnoreCase(newReason.getDisplayName()) )) { 164 newReason.addValue(this.m_sFilePath); 165 this.m_reason = newReason; 166 } 167 } 168 169 172 public void actionPerformed(ActionEvent ae) { 173 if(ae.getActionCommand().equals("COMBO")) { 174 Reason newReason = null; 175 String sReasonName = (String ) this.m_reasonComboBox.getSelectedItem(); 176 177 newReason = this.getReasonByName(sReasonName); 178 if(newReason!=null) { 179 this.setReason(newReason); 180 this.m_reasonComboBox.setSelectedItem(this.m_reason.getDisplayName()); 181 } else { 182 if(this.m_reason!=null) { 183 this.m_reasonComboBox.setSelectedItem(this.m_reason.getDisplayName()); 184 } 185 } 186 this.checkValid(); 187 } 188 } 189 190 public void refreshReasons() { 191 List aUseableReasons = new ArrayList(); 192 aUseableReasons.add(""); 193 List reasons = this.m_list.getReasons(); 194 for(int i=0; i<reasons.size(); i++) { 195 Reason reason = (Reason) reasons.get(i); 196 if(reason.isValidReasonForResource(m_vfs, m_sFilePath)) { 197 aUseableReasons.add( reason.getDisplayName() ); 198 } 199 } 200 String [] aReasons = new String [aUseableReasons.size()]; 201 for(int i=0; i<aUseableReasons.size(); i++) { 202 aReasons[i] = (String ) aUseableReasons.get(i); 203 } 204 205 String sSelectedReason = ""; 206 if(this.m_reasonComboBox==null) { 207 if(this.m_reason==null && aReasons.length==2) { 208 sSelectedReason = aReasons[1]; 209 210 } else if(this.m_reason!=null) { 211 sSelectedReason = this.m_reason.getDisplayName(); 212 } 213 this.m_reasonComboBox = this.getCombo(sSelectedReason, aReasons); 214 } else { 215 this.remove(this.m_reasonComboBox); 216 sSelectedReason = (String ) this.m_reasonComboBox.getSelectedItem(); 217 if(this.m_reason==null && sSelectedReason.equals("") && aReasons.length==2) { 218 sSelectedReason = aReasons[1]; 219 } else if(this.m_reason!=null) { 220 sSelectedReason = this.m_reason.getDisplayName(); 221 } 222 this.m_reasonComboBox = this.getCombo(sSelectedReason, aReasons); 223 } 224 this.setReason(this.getReasonByName(sSelectedReason)); 225 this.add(m_reasonComboBox); 226 this.checkValid(); 227 this.revalidate(); 228 } 229 230 233 public void layoutContainer(Container arg0) { 234 this.m_resourceLabel.setSize(m_nWidth-170, 20); 235 this.m_resourceLabel.setLocation(0,0); 236 this.m_errorLabel.setSize(20,20); 237 this.m_errorLabel.setLocation(m_nWidth-170, 0); 238 this.m_reasonComboBox.setSize(148, 18); 239 this.m_reasonComboBox.setLocation(m_nWidth-149, 1); 240 } 241 242 private JComboBox getCombo(String sSelectedItem, String [] aReasons) { 243 JComboBox combo = new JComboBox(aReasons); 244 combo.setSelectedItem(sSelectedItem); 245 combo.addActionListener(this); 246 combo.setActionCommand("COMBO"); 247 return combo; 248 } 249 250 253 public void mouseClicked(MouseEvent me) { 254 if(me.getSource()==this.m_resourceLabel || me.getSource()==this.m_errorLabel) { 255 this.m_list.cellSelected(this); 256 } 257 } 258 259 262 public Dimension getPreferredSize() { 263 return new Dimension(m_nWidth, 20); 264 } 265 266 269 public Dimension minimumLayoutSize(Container arg0) { 270 return this.getPreferredSize(); 271 } 272 273 276 public Dimension preferredLayoutSize(Container arg0) { 277 return this.getPreferredSize(); 278 } 279 280 283 public ResourceSelectionCell() { 284 super(); 285 } 286 287 290 private ResourceSelectionCell(boolean arg0) { 291 super(arg0); 292 } 293 294 297 private ResourceSelectionCell(LayoutManager arg0) { 298 super(arg0); 299 } 300 301 305 private ResourceSelectionCell(LayoutManager arg0, boolean arg1) { 306 super(arg0, arg1); 307 } 308 309 312 public void removeLayoutComponent(Component arg0) { 313 } 314 315 318 public void addLayoutComponent(String arg0, Component arg1) { 319 } 320 321 324 public void mouseEntered(MouseEvent arg0) { 325 } 326 327 330 public void mouseExited(MouseEvent arg0) { 331 } 332 333 336 public void mousePressed(MouseEvent arg0) { 337 } 338 339 342 public void mouseReleased(MouseEvent arg0) { 343 } 344 345 } 346 | Popular Tags |