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.window.session;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Color;
023: import java.awt.Component;
024:
025: import javax.swing.BorderFactory;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028: import javax.swing.JToolBar;
029:
030: import org.openharmonise.him.actions.*;
031: import org.openharmonise.him.actions.sync.*;
032: import org.openharmonise.vfs.*;
033: import org.openharmonise.vfs.context.*;
034: import org.openharmonise.vfs.event.*;
035:
036: /**
037: * @author Matthew Large
038: *
039: */
040: public class SessionWindow extends JPanel implements
041: VirtualFileListener, ContextListener {
042:
043: private static SessionWindow m_instance = null;
044:
045: private SessionInformation m_infoPanel = null;
046:
047: private VirtualFile m_vfListeningFile = null;
048:
049: private HIMAction m_syncButton = null;
050: private HIMAction m_discardButton = null;
051:
052: private SessionWindow() {
053: super ();
054: this .setup();
055: }
056:
057: public static SessionWindow getInstance() {
058: if (m_instance == null) {
059: m_instance = new SessionWindow();
060: }
061: return m_instance;
062: }
063:
064: private void setup() {
065: ContextHandler.getInstance().addListener(
066: ContextType.CONTEXT_FILES, this );
067: ContextHandler.getInstance().addListener(
068: ContextType.CONTEXT_SESSION_EVENT, this );
069:
070: this .setBackground(Color.WHITE);
071: this .setBorder(BorderFactory.createLineBorder(Color.black));
072:
073: BorderLayout layout = new BorderLayout();
074: this .setLayout(layout);
075:
076: JToolBar toolBar = new JToolBar();
077: toolBar.setFloatable(false);
078:
079: m_syncButton = new ActionSynchronize(this );
080: toolBar.add(m_syncButton.getButton());
081: m_syncButton.setEnabled(false);
082:
083: toolBar.addSeparator();
084:
085: m_discardButton = new ActionDiscard(this );
086: toolBar.add(m_discardButton.getButton());
087: m_discardButton.setEnabled(false);
088:
089: this .add(toolBar, BorderLayout.PAGE_START);
090:
091: this .m_infoPanel = new SessionInformation();
092: JScrollPane scroller = new JScrollPane(this .m_infoPanel,
093: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
094: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
095:
096: this .add(scroller, BorderLayout.CENTER);
097: }
098:
099: protected void addEntry(VirtualFile vfFile, String sReason) {
100: this .m_infoPanel.addEntry(vfFile, sReason);
101: }
102:
103: public void removeEntry(SessionEntry entry) {
104: if (this .getSelectedEntry() == entry) {
105: this .m_discardButton.setEnabled(false);
106: this .m_syncButton.setEnabled(false);
107: }
108: this .m_infoPanel.removeEntry(entry);
109: }
110:
111: protected void entrySelected(SessionEntry entry) {
112: if (entry.isVirtualFileChanged()) {
113: this .m_syncButton.setEnabled(true);
114: this .m_discardButton.setEnabled(true);
115: } else {
116: this .m_syncButton.setEnabled(false);
117: this .m_discardButton.setEnabled(false);
118: }
119: }
120:
121: public SessionEntry getSelectedEntry() {
122: return this .m_infoPanel.getSelectedEntry();
123: }
124:
125: /* (non-Javadoc)
126: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
127: */
128: public void contextMessage(ContextEvent ce) {
129: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_FILES) {
130: if (this .m_vfListeningFile != null) {
131: this .m_vfListeningFile
132: .removeVirtualFileListener((VirtualFileListener) this );
133: }
134: this .m_vfListeningFile = ce.getVFS().getVirtualFile(
135: ce.getPath()).getResource();
136: if (this .m_vfListeningFile != null) {
137: this .m_vfListeningFile
138: .addVirtualFileListener((VirtualFileListener) this );
139: }
140: } else if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SESSION_EVENT) {
141: SessionEventData sed = (SessionEventData) ce
142: .getContextData();
143: if (sed != null) {
144: if (sed.getEventType() == SessionEventData.RESOURCE_SUBMITTED) {
145: SessionEntry entry = this .getEntry(ce.getPath(), ce
146: .getVFS());
147: if (entry != null) {
148: this .removeEntry(entry);
149: }
150: this .addEntry(ce.getVFS().getVirtualFile(
151: ce.getPath()).getResource(), ce
152: .getMessage());
153: } else {
154: this .addEntry(ce.getVFS().getVirtualFile(
155: ce.getPath()).getResource(), ce
156: .getMessage());
157: }
158: }
159: }
160: }
161:
162: /* (non-Javadoc)
163: * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
164: */
165: public void virtualFileChanged(VirtualFileEvent vfe) {
166: if (vfe.getEventType() == VirtualFileEvent.FILE_METADATA_CHANGED) {
167: this .addEntry(this .m_vfListeningFile, "metadata edited");
168: } else if (vfe.getEventType() == VirtualFileEvent.FILE_CONTENT_CHANGED) {
169: this .addEntry(this .m_vfListeningFile, "content edited");
170: }
171: this .validateTree();
172: }
173:
174: private SessionEntry getEntry(String sPath,
175: AbstractVirtualFileSystem vfs) {
176: SessionEntry entry = null;
177: for (int i = 0; i < this .m_infoPanel.getComponentCount(); i++) {
178: Component comp = this .m_infoPanel.getComponent(i);
179: if (comp instanceof SessionEntry
180: && ((SessionEntry) comp).getPath().equals(sPath)
181: && ((SessionEntry) comp).getVFS() == vfs) {
182: entry = (SessionEntry) comp;
183: break;
184: }
185: }
186: return entry;
187: }
188:
189: }
|