KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > window > session > SessionWindow


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 package org.openharmonise.him.window.session;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24
25 import javax.swing.BorderFactory JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.JToolBar JavaDoc;
29
30 import org.openharmonise.him.actions.*;
31 import org.openharmonise.him.actions.sync.*;
32 import org.openharmonise.vfs.*;
33 import org.openharmonise.vfs.context.*;
34 import org.openharmonise.vfs.event.*;
35
36
37 /**
38  * @author Matthew Large
39  *
40  */

41 public class SessionWindow extends JPanel JavaDoc implements VirtualFileListener, ContextListener {
42     
43     private static SessionWindow m_instance = null;
44     
45     private SessionInformation m_infoPanel = null;
46     
47     private VirtualFile m_vfListeningFile = null;
48     
49     private HIMAction m_syncButton = null;
50     private HIMAction m_discardButton = null;
51
52     private SessionWindow() {
53         super();
54         this.setup();
55     }
56     
57     public static SessionWindow getInstance() {
58         if(m_instance==null) {
59             m_instance = new SessionWindow();
60         }
61         return m_instance;
62     }
63     
64     private void setup() {
65         ContextHandler.getInstance().addListener(ContextType.CONTEXT_FILES, this);
66         ContextHandler.getInstance().addListener(ContextType.CONTEXT_SESSION_EVENT, this);
67         
68         this.setBackground(Color.WHITE);
69         this.setBorder(BorderFactory.createLineBorder(Color.black));
70
71         BorderLayout JavaDoc layout = new BorderLayout JavaDoc();
72         this.setLayout(layout);
73         
74         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
75         toolBar.setFloatable(false);
76
77         m_syncButton = new ActionSynchronize(this);
78         toolBar.add(m_syncButton.getButton());
79         m_syncButton.setEnabled(false);
80         
81         toolBar.addSeparator();
82
83         m_discardButton = new ActionDiscard(this);
84         toolBar.add(m_discardButton.getButton());
85         m_discardButton.setEnabled(false);
86         
87         this.add(toolBar, BorderLayout.PAGE_START);
88
89         this.m_infoPanel = new SessionInformation();
90         JScrollPane JavaDoc scroller = new JScrollPane JavaDoc(this.m_infoPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
91         
92         this.add(scroller, BorderLayout.CENTER);
93     }
94     
95     protected void addEntry(VirtualFile vfFile, String JavaDoc sReason) {
96         this.m_infoPanel.addEntry(vfFile, sReason);
97     }
98     
99     public void removeEntry(SessionEntry entry) {
100         if(this.getSelectedEntry()==entry) {
101             this.m_discardButton.setEnabled(false);
102             this.m_syncButton.setEnabled(false);
103         }
104         this.m_infoPanel.removeEntry(entry);
105     }
106     
107     protected void entrySelected(SessionEntry entry) {
108         if(entry.isVirtualFileChanged()) {
109             this.m_syncButton.setEnabled(true);
110             this.m_discardButton.setEnabled(true);
111         } else {
112             this.m_syncButton.setEnabled(false);
113             this.m_discardButton.setEnabled(false);
114         }
115     }
116     
117     public SessionEntry getSelectedEntry() {
118         return this.m_infoPanel.getSelectedEntry();
119     }
120
121     /* (non-Javadoc)
122      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
123      */

124     public void contextMessage(ContextEvent ce) {
125         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_FILES) {
126             if(this.m_vfListeningFile!=null) {
127                 this.m_vfListeningFile.removeVirtualFileListener((VirtualFileListener) this);
128             }
129             this.m_vfListeningFile = ce.getVFS().getVirtualFile(ce.getPath()).getResource();
130             if(this.m_vfListeningFile!=null) {
131                 this.m_vfListeningFile.addVirtualFileListener((VirtualFileListener) this);
132             }
133         } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_SESSION_EVENT) {
134             SessionEventData sed = (SessionEventData) ce.getContextData();
135             if(sed!=null) {
136                 if(sed.getEventType()==SessionEventData.RESOURCE_SUBMITTED) {
137                     SessionEntry entry = this.getEntry(ce.getPath(), ce.getVFS());
138                     if(entry!=null) {
139                         this.removeEntry(entry);
140                     }
141                     this.addEntry(ce.getVFS().getVirtualFile(ce.getPath()).getResource(), ce.getMessage());
142                 } else {
143                     this.addEntry(ce.getVFS().getVirtualFile(ce.getPath()).getResource(), ce.getMessage());
144                 }
145             }
146         }
147     }
148
149     /* (non-Javadoc)
150      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
151      */

152     public void virtualFileChanged(VirtualFileEvent vfe) {
153         if(vfe.getEventType()==VirtualFileEvent.FILE_METADATA_CHANGED) {
154             this.addEntry(this.m_vfListeningFile, "metadata edited");
155         } else if(vfe.getEventType()==VirtualFileEvent.FILE_CONTENT_CHANGED) {
156             this.addEntry(this.m_vfListeningFile, "content edited");
157         }
158         this.validateTree();
159     }
160     
161     private SessionEntry getEntry(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
162         SessionEntry entry = null;
163         for(int i=0; i<this.m_infoPanel.getComponentCount(); i++) {
164             Component JavaDoc comp = this.m_infoPanel.getComponent(i);
165             if(comp instanceof SessionEntry && ((SessionEntry)comp).getPath().equals(sPath) && ((SessionEntry)comp).getVFS()==vfs) {
166                 entry = (SessionEntry) comp;
167                 break;
168             }
169         }
170         return entry;
171     }
172
173 }
174
Popular Tags