| 1 19 package org.openharmonise.him.window.session; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Component ; 24 25 import javax.swing.BorderFactory ; 26 import javax.swing.JPanel ; 27 import javax.swing.JScrollPane ; 28 import javax.swing.JToolBar ; 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 41 public class SessionWindow extends JPanel 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 layout = new BorderLayout (); 72 this.setLayout(layout); 73 74 JToolBar toolBar = new JToolBar (); 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 scroller = new JScrollPane (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 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 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 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 sPath, AbstractVirtualFileSystem vfs) { 162 SessionEntry entry = null; 163 for(int i=0; i<this.m_infoPanel.getComponentCount(); i++) { 164 Component 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 |