1 19 package org.openharmonise.him.actions; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.net.*; 24 import java.rmi.RemoteException ; 25 import java.util.*; 26 27 import javax.swing.*; 28 import javax.xml.rpc.ServiceException ; 29 30 import org.openharmonise.him.actions.rules.*; 31 import org.openharmonise.him.harmonise.UserConfigClient; 32 import org.openharmonise.him.window.session.SessionEventData; 33 import org.openharmonise.vfs.*; 34 import org.openharmonise.vfs.authentication.AuthInfo; 35 import org.openharmonise.vfs.context.*; 36 import org.openharmonise.vfs.servers.*; 37 38 39 46 public abstract class AbstractHIMAction implements ActionListener { 47 48 51 private VirtualFile m_vfFile = null; 52 53 56 private JButton m_button = null; 57 58 61 private JMenuItem m_menuItem = null; 62 63 66 private ArrayList m_aEnableRules = new ArrayList(); 67 68 71 protected boolean m_bShow = false; 72 73 76 protected boolean m_bUserChecked = false; 77 78 81 public AbstractHIMAction() { 82 super(); 83 } 84 85 90 public AbstractHIMAction(VirtualFile vfFile) { 91 super(); 92 m_vfFile = vfFile; 93 } 94 95 98 public abstract void actionPerformed(ActionEvent arg0); 99 100 105 protected VirtualFile getPrimaryFile() { 106 VirtualFile vfFile = null; 107 108 if(this.m_vfFile!=null) { 109 vfFile = this.m_vfFile; 110 } else { 111 vfFile = this.getLastContextFile(); 112 } 113 114 return vfFile; 115 } 116 117 122 protected VirtualFile getLastContextFile() { 123 ContextEvent ce = ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_FILES); 124 return ce.getVFS().getVirtualFile(ce.getPath()).getResource(); 125 } 126 127 132 protected VirtualFile getPrimaryDirectory() { 133 VirtualFile vfFile = null; 134 135 if(this.m_vfFile!=null) { 136 vfFile = this.m_vfFile; 137 } else { 138 vfFile = this.getLastContextDirectory(); 139 } 140 141 return vfFile; 142 } 143 144 149 protected VirtualFile getLastContextDirectory() { 150 ContextEvent ce = ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_DIRS); 151 if(ce!=null && ce.getVFS()!=null) { 152 return ce.getVFS().getVirtualFile(ce.getPath()).getResource(); 153 } else { 154 return null; 155 } 156 } 157 158 161 public JButton getButton() { 162 if(this.m_button==null) { 163 this.m_button = new JButton(this.getText()); 164 this.m_button.setToolTipText(this.getToolTip()); 165 this.m_button.setIcon(this.getIcon()); 166 this.m_button.addActionListener(this); 167 168 String fontName = "Dialog"; 169 int fontSize = 11; 170 Font font = new Font(fontName, Font.PLAIN, fontSize); 171 this.m_button.setFont(font); 172 Dimension dim = new Dimension(this.m_button.getPreferredSize().width, 25); 173 this.m_button.setPreferredSize(dim); 174 } 175 176 return this.m_button; 177 } 178 179 182 public JMenuItem getMenuItem() { 183 if(this.m_menuItem==null) { 184 this.m_menuItem = new JMenuItem(this.getText()); 185 this.m_menuItem.setToolTipText(this.getToolTip()); 186 this.m_menuItem.setIcon(this.getIcon()); 187 this.m_menuItem.addActionListener(this); 188 this.m_menuItem.setMnemonic(this.getMnemonic().charAt(0)); 189 190 String fontName = "Dialog"; 191 int fontSize = 11; 192 Font font = new Font(fontName, Font.PLAIN, fontSize); 193 this.m_menuItem.setFont(font); 194 } 195 196 return this.m_menuItem; 197 } 198 199 204 protected void addEnableRule(EnableRule rule) { 205 this.m_aEnableRules.add(rule); 206 } 207 208 216 public boolean isEnabled(ContextEvent ce) { 217 boolean bEnabled = true; 218 219 if(ce.CONTEXT_TYPE!=ContextType.CONTEXT_TABS) { 220 VirtualFile vfFile = ce.getVFS().getVirtualFile(ce.getPath()).getResource(); 221 if(this.m_aEnableRules.size()>0) { 222 bEnabled = false; 223 Iterator itor = this.m_aEnableRules.iterator(); 224 while(itor.hasNext()) { 225 if( ((EnableRule)itor.next()).isEnabled(vfFile) ) { 226 bEnabled = true; 227 } 228 } 229 } 230 } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) { 231 if(this.m_aEnableRules.size()>0) { 232 bEnabled = false; 233 Iterator itor = this.m_aEnableRules.iterator(); 234 while(itor.hasNext()) { 235 EnableRule tempRule = (EnableRule)itor.next(); 236 if( tempRule instanceof TabRule && ((TabRule)tempRule).isEnabled(ce.getMessage().trim()) ) { 237 bEnabled = true; 238 } 239 } 240 } 241 } else { 242 bEnabled=true; 243 } 244 245 this.setEnabled(bEnabled); 246 247 return bEnabled; 248 } 249 250 255 public void setEnabled(boolean bEnabled) { 256 if(this.m_button!=null) { 257 this.m_button.setEnabled(bEnabled); 258 } 259 if(this.m_menuItem!=null) { 260 this.m_menuItem.setEnabled(bEnabled); 261 } 262 } 263 264 269 public abstract String getText(); 270 271 276 public abstract String getToolTip(); 277 278 283 public abstract Icon getIcon(); 284 285 290 public abstract String getMnemonic(); 291 292 300 protected void fireSessionEvent(String sMessage, AbstractVirtualFileSystem vfs, String sPath, String sSessionEventType) { 301 try { 302 SessionEventData sed = new SessionEventData(sSessionEventType); 303 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SESSION_EVENT, sMessage, vfs, sPath); 304 ce.setContextData(sed); 305 ContextHandler.getInstance().fireContextEvent(ce); 306 } catch (Exception e) { 307 e.printStackTrace(); 308 } 309 } 310 315 protected void checkUser() { 316 Server server = null; 317 server = ServerList.getInstance().getHarmoniseServer(); 318 URI uri = server.getURI(); 319 320 String sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService"; 321 URL url = null; 322 try { 323 url = new URL(sURI); 324 } catch (MalformedURLException e2) { 325 e2.printStackTrace(); 326 System.exit(1); 327 } 328 329 AuthInfo auth = server.getVFS().getAuthentication(); 330 331 try { 332 if( UserConfigClient.isSuperUser(url, auth.getUsername(), auth.getPassword()) ) { 333 this.m_bShow=true; 334 } 335 } catch (RemoteException e) { 336 e.printStackTrace(); 337 } catch (ServiceException e) { 338 e.printStackTrace(); 339 } 340 this.m_bUserChecked=true; 341 } 342 } 343
| Popular Tags
|