KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > displaycomponents > table > VersionEntry


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.him.displaycomponents.table;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import javax.swing.*;
26
27 import org.openharmonise.commons.xml.namespace.*;
28 import org.openharmonise.him.configuration.*;
29 import org.openharmonise.him.editors.*;
30 import org.openharmonise.him.harmonise.*;
31 import org.openharmonise.him.window.menus.*;
32 import org.openharmonise.him.window.messages.*;
33 import org.openharmonise.swing.FontManager;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.context.*;
36 import org.openharmonise.vfs.event.*;
37 import org.openharmonise.vfs.gui.*;
38 import org.openharmonise.vfs.metadata.*;
39 import org.openharmonise.vfs.metadata.value.*;
40
41
42 /**
43  * Component for displaying resource information in an expanded table
44  * entry.
45  *
46  * @author Matthew Large
47  * @version $Revision: 1.3 $
48  *
49  */

50 public class VersionEntry
51     extends JPanel
52     implements MouseListener, VirtualFileListener, ContextListener {
53
54     /**
55      *
56      */

57     protected VersionEntry() {
58         super();
59     }
60
61     /**
62      * Table.
63      */

64     private TableView m_table = null;
65     
66     /**
67      * Expanded table entry that this is part of.
68      */

69     private TableEntry m_parentEntry = null;
70
71     /**
72      * Full path to resource that this entry represents.
73      */

74     private String JavaDoc m_sFilepath = null;
75     
76     /**
77      * Virtual file system for this entry.
78      */

79     private AbstractVirtualFileSystem m_vfs = null;
80
81     /**
82      * Selected highlight colour.
83      */

84     private Color m_selectedColor = new Color(173, 169, 143);
85
86     /**
87      * Icon on the right.
88      */

89     private JLabel m_rightIcon = null;
90     
91     /**
92      * Title.
93      */

94     private JLabel m_title = null;
95
96     /**
97      * Constructs a new verison entry.
98      *
99      * @param parentEntry Expanded table entry that this is part of
100      * @param vfs Virtual file system for this entry
101      * @param sFilepath Full path to resource that this entry represents
102      * @param sTitle Title
103      */

104     public VersionEntry(
105         TableEntry parentEntry,
106         AbstractVirtualFileSystem vfs,
107         String JavaDoc sFilepath,
108         String JavaDoc sTitle) {
109         super();
110         this.m_sFilepath = sFilepath;
111         this.m_vfs = vfs;
112         this.m_parentEntry = parentEntry;
113         this.m_table = parentEntry.getTableView();
114         this.setup(sTitle, true);
115     }
116
117     /**
118      * Constructs a new verison entry.
119      *
120      * @param parentEntry Expanded table entry that this is part of
121      * @param vfs Virtual file system for this entry
122      * @param sFilepath Full path to resource that this entry represents
123      * @param sTitle Title
124      * @param bDisplayIcon Display Icon
125      */

126     public VersionEntry(
127         TableEntry parentEntry,
128         AbstractVirtualFileSystem vfs,
129         String JavaDoc sFilepath,
130         String JavaDoc sTitle,
131         boolean bDisplayIcon) {
132         super();
133         this.m_sFilepath = sFilepath;
134         this.m_vfs = vfs;
135         this.m_parentEntry = parentEntry;
136         this.m_table = parentEntry.getTableView();
137         this.setup(sTitle, bDisplayIcon);
138     }
139
140     /**
141      * Returns the expanded table entry that this is part of.
142      *
143      * @return Table entry
144      */

145     public TableEntry getParentTableEntry() {
146         return this.m_parentEntry;
147     }
148
149     /**
150      * Clears all resources that this class is holding onto making it
151      * ready to be destroyed.
152      *
153      */

154     public void clearToDestroy() {
155         ContextHandler.getInstance().removeListener(ContextType.CONTEXT_FILENAME_DISPLAY, this);
156         VirtualFile vfFile = this.m_vfs.getVirtualFile(this.m_sFilepath).getResource();
157         if (vfFile != null) {
158             vfFile.removeVirtualFileListener(this);
159         }
160     }
161
162     /**
163      * Configures the table view.
164      *
165      * @param sTitle Title
166      * @param bDisplayIcon true if there is a display icon available
167      */

168     private void setup(String JavaDoc sTitle, boolean bDisplayIcon) {
169         ContextHandler.getInstance().addListener(ContextType.CONTEXT_FILENAME_DISPLAY, this);
170         this.addMouseListener(this);
171
172         VirtualFile vfFile = this.m_vfs.getVirtualFile(this.m_sFilepath).getResource();
173         VirtualFileSystemView vfsView = this.m_vfs.getVirtualFileSystemView();
174
175         vfFile.addVirtualFileListener(this);
176
177         Icon iIcon = null;
178         if (bDisplayIcon) {
179             iIcon = vfsView.getIcon(vfFile);
180         } else {
181             iIcon = IconManager.getInstance().getIcon("16-blank.gif");
182         }
183         String JavaDoc sRawDate = vfsView.getModificationDate(vfFile);
184 //
185
// String fontName = "Arial Unicode MS";
186
// int fontSize = 11;
187
// Font font = new Font(fontName, Font.PLAIN, fontSize);
188
// Font fontBold = new Font(fontName, Font.BOLD, fontSize);
189

190         this.setPreferredSize(new Dimension(160, 40));
191         this.setSize(new Dimension(160, 40));
192         this.setBackground(Color.WHITE);
193         FlowLayout fl2 = new FlowLayout(FlowLayout.LEFT);
194         fl2.setHgap(0);
195         fl2.setVgap(0);
196         this.setLayout(fl2);
197
198         String JavaDoc sDisplayName = this.m_vfs.getVirtualFileSystemView().getDisplayName(vfFile);
199         String JavaDoc sValue = ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY");
200         if(sValue!=null && sValue.length()>0 && (sValue.equals("FILENAME") || this.m_sFilepath.startsWith( HarmonisePaths.PATH_USERS ))) {
201             String JavaDoc sFilename = this.m_vfs.getVirtualFileSystemView().getLogicalFileName(this.m_vfs.getVirtualFile(this.m_sFilepath).getResource());
202             m_title =
203                 new JLabel( sFilename );
204         } else if(sValue!=null && sValue.length()>0 && (sValue.equals("ID"))) {
205             String JavaDoc sFilename = this.m_vfs.getVirtualFileSystemView().getLogicalFileName(this.m_vfs.getVirtualFile(this.m_sFilepath).getResource());
206             String JavaDoc sID = "";
207                 
208             PropertyInstance propInst = this.m_vfs.getVirtualFile(this.m_sFilepath).getResource().getProperty(NamespaceType.OHRM.getURI(), "harmonise-id");
209             if(propInst!=null && propInst.getValues().size()>0) {
210                 if(propInst.getValues().get(0) instanceof StringValue) {
211                     StringValue value = (StringValue) propInst.getValues().get(0);
212                     if(value.getValue()!=null && value.getValue().length()>0) {
213                         sID = value.getValue() + ": ";
214                     }
215                 }
216             }
217             this.m_title= new JLabel( sID + sFilename );
218         } else {
219             m_title =
220                 new JLabel( sDisplayName );
221         }
222         if(vfFile.getState().equals(VirtualFile.STATE_LIVE)) {
223             m_title.setFont(FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE_BOLD));
224         } else {
225             m_title.setFont(FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE));
226         }
227         
228         m_title.setPreferredSize(new Dimension(129, 16));
229         m_title.setIcon(iIcon);
230         if(vfFile.getState().equals(VirtualFile.STATE_LIVE)) {
231             m_title.setToolTipText(sDisplayName+" (published)");
232         } else if(vfFile.getState().equals(VirtualFile.STATE_HISTORICAL)) {
233             m_title.setToolTipText(sDisplayName+" (historical)");
234         } else {
235             m_title.setToolTipText(sDisplayName);
236         }
237         m_title.addMouseListener(this);
238         this.add(m_title);
239
240         m_rightIcon = new JLabel();
241         m_rightIcon.setPreferredSize(new Dimension(16, 16));
242         if (vfFile.isChanged()) {
243             m_rightIcon.setIcon(
244                 IconManager.getInstance().getIcon("16-command-sync-all.gif"));
245         } else {
246             m_rightIcon.setIcon(
247                 IconManager.getInstance().getIcon("16-blank.gif"));
248         }
249         m_rightIcon.addMouseListener(this);
250         this.add(m_rightIcon);
251
252         JLabel date = new JLabel(sRawDate);
253         date.setFont(FontManager.getInstance().getFont(FontManager.FONT_STANDARD));
254         date.setPreferredSize(new Dimension(145, 16));
255         date.setIcon(IconManager.getInstance().getIcon("16-blank.gif"));
256         date.addMouseListener(this);
257         this.add(date);
258         this.doLayout();
259     }
260
261     /* (non-Javadoc)
262      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
263      */

264     public void mouseClicked(MouseEvent me) {
265         if(me.getClickCount()>1) {
266             VirtualFile vfFile = this.m_vfs.getVirtualFile(this.m_sFilepath).getResource();
267             if(vfFile.isDirectory()) {
268                 if(vfFile.isVersionable() && vfFile.getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null) {
269                     this.m_table.openPathInTree(((VersionedVirtualFile)vfFile).getLiveVersionPath());
270                 } else {
271                     this.m_table.openPathInTree(this.m_sFilepath);
272                 }
273             } else if(vfFile.getFullPath().startsWith(HarmonisePaths.PATH_REPORTS_OUTPUT)) {
274                 EditorController.getInstance().preview(this.m_sFilepath, this.m_vfs);
275             } else if(vfFile.isLocked() && vfFile.getVFS().getVirtualFile(vfFile.getLockOwner())!=null && !vfFile.getVFS().getVirtualFile(vfFile.getLockOwner()).getResource().getFileName().trim().equals(vfFile.getVFS().getAuthentication().getUsername().trim())) {
276                 MessageHandler.getInstance().fireMessageEvent("Cannot open resource \"" + vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile) + "\" for editing, previewing instead.", MessageHandler.TYPE_INFORMATION);
277                 EditorController.getInstance().preview(this.m_sFilepath, this.m_vfs);
278             } else if(vfFile.isVersionable() && vfFile.getState().equals(VirtualFile.STATE_LIVE) && ((VersionedVirtualFile)vfFile).getPendingVersionPath()!=null) {
279                 MessageHandler.getInstance().fireMessageEvent("Cannot open resource \"" + vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile) + "\" for editing, previewing instead.", MessageHandler.TYPE_INFORMATION);
280                 EditorController.getInstance().preview(this.m_sFilepath, this.m_vfs);
281             } else {
282                 EditorController.getInstance().open(this.m_sFilepath, this.m_vfs);
283             }
284         } else {
285             if (me.getSource() == this
286                 || this.isChildComponent(
287                     (Component) me.getSource()) ) {
288                 if (this.getBackground() == m_selectedColor) {
289                     this.selectEntry();
290                 } else {
291                     this.selectEntry();
292                 }
293                 this.m_table.entrySelected(this);
294             }
295         }
296         if(me.getButton()==MouseEvent.BUTTON3) {
297             FileContextMenu menu = new FileContextMenu(this.m_sFilepath, this.m_vfs);
298             Point pt = me.getPoint();
299             menu.show(this, pt.x, pt.y);
300         }
301     }
302
303     /**
304      * Selects this entry.
305      *
306      */

307     protected void selectEntry() {
308         this.setBackground(m_selectedColor);
309         this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
310         this.m_parentEntry.deselectOtherEntries(this);
311     }
312
313     /**
314      * Unselects this entry.
315      *
316      */

317     protected void deselectEntry() {
318         this.setBackground(Color.WHITE);
319
320         this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
321     }
322
323     /**
324      * Checks if this is a child of a given component.
325      *
326      * @param comp Component to check against
327      * @return true if this is a child of the given component
328      */

329     private boolean isChildComponent(Component comp) {
330         Component[] comps = this.getComponents();
331         for (int i = 0; i < comps.length; i++) {
332             if (comp == comps[i]) {
333                 return true;
334             }
335         }
336         return false;
337     }
338
339     /**
340      * Returns the full path for the resource this entry represents.
341      *
342      * @return Full path
343      */

344     public String JavaDoc getPath() {
345         return this.m_sFilepath;
346     }
347
348     /**
349      * Returns the virtual file system for the resource this entry represents.
350      *
351      * @return Virtual file system
352      */

353     public AbstractVirtualFileSystem getVFS() {
354         return this.m_vfs;
355     }
356
357     /* (non-Javadoc)
358      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
359      */

360     public void mouseEntered(MouseEvent arg0) {
361     }
362
363     /* (non-Javadoc)
364      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
365      */

366     public void mouseExited(MouseEvent arg0) {
367     }
368
369     /* (non-Javadoc)
370      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
371      */

372     public void mousePressed(MouseEvent arg0) {
373     }
374
375     /* (non-Javadoc)
376      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
377      */

378     public void mouseReleased(MouseEvent arg0) {
379     }
380
381     /* (non-Javadoc)
382      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
383      */

384     public void virtualFileChanged(VirtualFileEvent vfe) {
385         if (vfe.getEventType() == VirtualFileEvent.FILE_CONTENT_CHANGED
386             || vfe.getEventType() == VirtualFileEvent.FILE_METADATA_CHANGED) {
387             if (this.m_vfs.getVirtualFile(this.m_sFilepath).getResource().isChanged()) {
388                 this.m_rightIcon.setIcon(
389                     IconManager.getInstance().getIcon(
390                         "16-command-sync-all.gif"));
391                 ContextEvent ce =
392                     new ContextEvent(
393                         ContextType.CONTEXT_FILES,
394                         null,
395                         this.m_vfs,
396                         this.m_sFilepath);
397                 ContextHandler.getInstance().fireContextEvent(ce);
398             }
399         } else if (
400             vfe.getEventType() == VirtualFileEvent.FILE_SYNCHED) {
401             this.m_rightIcon.setIcon(
402                 IconManager.getInstance().getIcon("16-blank.gif"));
403         } else if (
404             vfe.getEventType() == VirtualFileEvent.FILE_CHANGES_DISCARDED) {
405             this.m_rightIcon.setIcon(
406                 IconManager.getInstance().getIcon("16-blank.gif"));
407         }
408         this.validate();
409     }
410
411     /* (non-Javadoc)
412      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
413      */

414     public void contextMessage(ContextEvent ce) {
415         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_FILENAME_DISPLAY) {
416             String JavaDoc sValue = ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY");
417             if(sValue!=null && sValue.length()>0 && (sValue.equals("FILENAME") || this.m_sFilepath.startsWith( HarmonisePaths.PATH_USERS ))) {
418                 String JavaDoc sFilename = this.m_vfs.getVirtualFileSystemView().getLogicalFileName(this.m_vfs.getVirtualFile(this.m_sFilepath).getResource());
419                 this.m_title.setText( sFilename );
420             } else if(sValue!=null && sValue.length()>0 && sValue.equals("ID")) {
421                 String JavaDoc sFilename = this.m_vfs.getVirtualFileSystemView().getLogicalFileName(this.m_vfs.getVirtualFile(this.m_sFilepath).getResource());
422                 String JavaDoc sID = "";
423                     
424                 PropertyInstance propInst = this.m_vfs.getVirtualFile(this.m_sFilepath).getResource().getProperty(NamespaceType.OHRM.getURI(), "harmonise-id");
425                 if(propInst!=null && propInst.getValues().size()>0) {
426                     if(propInst.getValues().get(0) instanceof StringValue) {
427                         StringValue value = (StringValue) propInst.getValues().get(0);
428                         if(value.getValue()!=null && value.getValue().length()>0) {
429                             sID = value.getValue() + ": ";
430                         }
431                     }
432                 }
433                 this.m_title.setText( sID + sFilename );
434             } else {
435                 String JavaDoc sDisplayName = this.m_vfs.getVirtualFileSystemView().getDisplayName(this.m_vfs.getVirtualFile(this.m_sFilepath).getResource());
436                 this.m_title.setText( sDisplayName );
437             }
438             this.revalidate();
439             this.repaint();
440         }
441     }
442
443 }
444
Popular Tags