1 19 package org.openharmonise.him.actions.file; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 28 import org.openharmonise.him.actions.*; 29 import org.openharmonise.him.actions.rules.*; 30 import org.openharmonise.him.configuration.*; 31 import org.openharmonise.him.context.StateHandler; 32 import org.openharmonise.him.editors.*; 33 import org.openharmonise.him.harmonise.*; 34 import org.openharmonise.him.window.messages.builders.*; 35 import org.openharmonise.him.window.session.*; 36 import org.openharmonise.swing.*; 37 import org.openharmonise.vfs.*; 38 import org.openharmonise.vfs.context.*; 39 import org.openharmonise.vfs.gui.*; 40 import org.openharmonise.vfs.status.*; 41 42 43 50 public class ActionNewFile extends AbstractHIMAction implements HIMAction { 51 52 public static String ACTION_NAME = "NEW_FILE"; 53 54 57 boolean m_bIsEnabled = false; 58 59 62 private HashMap m_namePathMapping = new HashMap(); 63 64 67 private HashMap m_nameIconMapping = new HashMap(); 68 69 72 private String m_sCurrentPath = ""; 73 74 77 public ActionNewFile() { 78 super(); 79 this.setup(); 80 } 81 82 85 public ActionNewFile(VirtualFile vfFile) { 86 super(vfFile); 87 this.setup(); 88 } 89 90 94 private void setup() { 95 RuleGroup andGroup = new RuleGroup(); 96 andGroup.setGroupType(RuleGroup.GROUPTYPE_AND); 97 super.addEnableRule(andGroup); 98 99 RuleGroup orGroup = new RuleGroup(); 100 orGroup.setGroupType(RuleGroup.GROUPTYPE_OR); 101 102 orGroup.addEnableRule( new PathRule("/webdav/Content/Documents") ); 103 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/flash") ); 104 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/audio") ); 105 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/movies") ); 106 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/office") ); 107 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/image") ); 108 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/Web Resources") ); 109 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/e-mail") ); 110 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/links") ); 111 orGroup.addEnableRule( new PathRule("/webdav/Content/Assets/pdf") ); 112 orGroup.addEnableRule( new PathRule("/webdav/Metadata/Properties") ); 113 orGroup.addEnableRule( new PathRule("/webdav/Metadata/Values") ); 114 orGroup.addEnableRule( new PathRule("/webdav/Newsletter/draft") ); 115 orGroup.addEnableRule( new PathRule("/webdav/Reports/Queries") ); 116 orGroup.addEnableRule( new PathRule("/webdav/Users") ); 117 orGroup.addEnableRule( new PathRule("/webdav/Users/admin") ); 118 orGroup.addEnableRule( new PathRule("/webdav/Users/public") ); 119 orGroup.addEnableRule( new PathRule("/webdav/Website/Page Definition") ); 120 orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Page Templates") ); 121 orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Object Templates") ); 122 orGroup.addEnableRule( new PathRule("/webdav/Website/Composition/Includes") ); 123 orGroup.addEnableRule( new PathRule("/webdav/Website/Display/XSLT") ); 124 orGroup.addEnableRule( new PathRule("/webdav/Website/Site Assets/FlashNav") ); 125 orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_DEFINITIONS ) ); 127 orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_PROPS ) ); 128 orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_WORKFLOW_STAGES ) ); 129 orGroup.addEnableRule( new PathRule( HarmonisePaths.PATH_REPORTS_QUERIES ) ); 130 131 andGroup.addEnableRule(orGroup); 132 133 EnableRule rule = new IsDirectoryRule(); 134 andGroup.addEnableRule( rule ); 135 136 this.m_namePathMapping.put("/webdav/Content/Assets/flash", "New Flash Movie"); 137 this.m_namePathMapping.put("/webdav/Content/Assets/movies", "New Movie"); 138 this.m_namePathMapping.put("/webdav/Content/Assets/office", "New Office Document"); 139 this.m_namePathMapping.put("/webdav/Content/Assets/image", "New Image"); 140 this.m_namePathMapping.put("/webdav/Content/Assets/Web Resources", "New Web Resource"); 141 this.m_namePathMapping.put("/webdav/Content/Assets/e-mail", "New E-mail Address"); 142 this.m_namePathMapping.put("/webdav/Content/Assets/links", "New Web Link"); 143 this.m_namePathMapping.put("/webdav/Content/Assets/pdf", "New PDF"); 144 this.m_namePathMapping.put("/webdav/Content/Documents", "New Document"); 145 this.m_namePathMapping.put("/webdav/Metadata/Properties", "New Property"); 146 this.m_namePathMapping.put("/webdav/Metadata/Values", "New Value"); 147 this.m_namePathMapping.put("/webdav/Newsletter/draft", "New Newsletter"); 148 this.m_namePathMapping.put("/webdav/Reports/Queries", "New Report"); 149 this.m_namePathMapping.put("/webdav/Users", "New User"); 150 this.m_namePathMapping.put( "/webdav/Website/Page Definition", "New Page Definition" ); 151 this.m_namePathMapping.put( "/webdav/Website/Composition/Page Templates", "New Page Template" ); 152 this.m_namePathMapping.put( "/webdav/Website/Composition/Object Templates", "New Object Template" ); 153 this.m_namePathMapping.put( "/webdav/Website/Composition/Includes", "New Include" ); 154 this.m_namePathMapping.put( "/webdav/Website/Display/XSLT", "New XSLT" ); 155 this.m_namePathMapping.put( "/webdav/Website/Site Assets/FlashNav", "New Flash Navigation" ); 156 this.m_namePathMapping.put( HarmonisePaths.PATH_REPORTS_QUERIES , "New Report Query"); 157 this.m_namePathMapping.put( HarmonisePaths.PATH_WORKFLOW_PROPS , "New Workflow"); 158 this.m_namePathMapping.put( HarmonisePaths.PATH_WORKFLOW_STAGES , "New WorkflowStage"); 159 160 161 this.m_nameIconMapping.put("/webdav/Content/Assets/flash", "16-flash.gif"); 162 this.m_nameIconMapping.put("/webdav/Content/Assets/movies", "16-movie.gif"); 163 this.m_nameIconMapping.put("/webdav/Content/Assets/office", "16-document.gif"); 164 this.m_nameIconMapping.put("/webdav/Content/Assets/image", "16-image.gif"); 165 this.m_nameIconMapping.put("/webdav/Content/Assets/Web Resources", "16-link.gif"); 166 this.m_nameIconMapping.put("/webdav/Content/Assets/e-mail", "16-email.gif"); 167 this.m_nameIconMapping.put("/webdav/Content/Assets/links", "16-link.gif"); 168 this.m_nameIconMapping.put("/webdav/Content/Assets/pdf", "16-pdf.gif"); 169 this.m_nameIconMapping.put("/webdav/Content/Documents", "16-document.gif"); 170 this.m_nameIconMapping.put("/webdav/Metadata/Properties", "16-property.gif"); 171 this.m_nameIconMapping.put("/webdav/Metadata/Values", "16-value.gif"); 172 this.m_nameIconMapping.put("/webdav/Newsletter/draft", "16-newsletter.gif"); 173 this.m_nameIconMapping.put("/webdav/Reports/Queries", "16-report.gif"); 174 this.m_nameIconMapping.put("/webdav/Users", "16-user.gif"); 175 this.m_nameIconMapping.put("/webdav/Website/Page Definition", "16-page-definition.gif"); 176 this.m_nameIconMapping.put("/webdav/Website/Composition/Page Templates", "16-template.gif"); 177 this.m_nameIconMapping.put("/webdav/Website/Composition/Object Templates", "16-object-template.gif"); 178 this.m_nameIconMapping.put( "/webdav/Website/Composition/Includes", "16-includes.gif" ); 179 this.m_nameIconMapping.put("/webdav/Website/Display/XSLT", "16-xslt.gif"); 180 this.m_nameIconMapping.put("/webdav/Website/Site Assets/FlashNav", "16-flash.gif"); 181 this.m_nameIconMapping.put( HarmonisePaths.PATH_REPORTS_QUERIES , "16-query.gif"); 182 this.m_nameIconMapping.put( HarmonisePaths.PATH_WORKFLOW_PROPS , "16-command-change-status.gif"); 183 this.m_nameIconMapping.put( HarmonisePaths.PATH_WORKFLOW_STAGES , "16-workflow-container.gif"); 184 185 } 186 187 190 public void actionPerformed(ActionEvent arg0) { 191 boolean bChange = (ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY")!=null && ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY").equals("DISPLAYNAME")); 192 193 StatusData statusOverall = new VFSStatus(); 194 195 if(bChange) { 196 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "FILENAME"); 197 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY); 198 ContextHandler.getInstance().fireContextEvent(ce); 199 } 200 String sName = ""; 201 if(this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_LINKS) || this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_EMAIL)) { 202 203 String sLink = ""; 204 String sTitle = "New Web Address"; 205 String sPath=null; 206 207 if(this.getLastContextDirectory().getFullPath().startsWith(HarmonisePaths.PATH_EMAIL)) { 208 sTitle = "New E-mail Address"; 209 } 210 211 JFrame frame = new JFrame(); 212 frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() ); 213 214 DualTextEntryDialog dialog = new DualTextEntryDialog(frame, sTitle); 215 dialog.setLabelText("Only use a-z, A-Z and 0-9 in name."); 216 dialog.setCaption1("Resource name"); 217 dialog.setCaption2("Address"); 218 dialog.show(); 219 sName = dialog.getText1Value(); 220 sLink = dialog.getText2Value(); 221 222 while(!sName.equals("") && !this.checkName(sName)) { 223 dialog = new DualTextEntryDialog(frame, "Resource name"); 224 dialog.setLabelText("Only use a-z, A-Z and 0-9 in name."); 225 dialog.setLabelColor(Color.RED); 226 dialog.setCaption1("Resource name"); 227 dialog.setCaption2("Address"); 228 dialog.show(); 229 sName = dialog.getText1Value(); 230 sLink = dialog.getText2Value(); 231 } 232 233 StateHandler.getInstance().addWait("NEW-FILE-ACTION"); 234 try { 235 if(!sName.equals("") && !sLink.equals("")) { 236 sPath = this.getLastContextDirectory().getFullPath() + "/" + sName; 237 238 StatusData status = EditorController.getInstance().createNew(sPath, sLink.getBytes(), this.getLastContextDirectory().getVFS()); 239 statusOverall.addStatusData(status); 240 if( status.isOK() ) { 241 VirtualFile vfNewFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource(); 242 super.fireSessionEvent("Created", this.getLastContextDirectory().getVFS(), sPath, SessionEventData.RESOURCE_CREATED); 243 } else { 244 } 245 } 246 } catch (Exception e) { 247 e.printStackTrace(System.err); 248 statusOverall.setStatusLevel(StatusData.LEVEL_ERROR); 249 } finally { 250 if(bChange) { 251 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME"); 252 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY); 253 ContextHandler.getInstance().fireContextEvent(ce); 254 } 255 StateHandler.getInstance().removeWait("NEW-FILE-ACTION"); 256 if(!sName.equals("") && !sLink.equals("")) { 257 StatusMessage statusMessage = new StatusMessage(ActionNewFile.ACTION_NAME, statusOverall); 258 statusMessage.setResourceTitle(sName); 259 statusMessage.setPath(sPath); 260 VFSMessageBuilder.getInstance().fireMessage( statusMessage ); 261 } 262 } 263 } else { 264 sName = this.getNewName(); 265 String sPath=null; 266 267 StateHandler.getInstance().addWait("NEW-FILE-ACTION"); 268 try { 269 if(!sName.equals("")) { 270 sPath = this.getLastContextDirectory().getFullPath() + "/" + sName; 271 272 StatusData status = EditorController.getInstance().createNew(sPath, this.getLastContextDirectory().getVFS()); 273 statusOverall.addStatusData(status); 274 if( status.isOK() ) { 275 VirtualFile vfNewFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource(); 276 super.fireSessionEvent("Created", this.getLastContextDirectory().getVFS(), sPath, SessionEventData.RESOURCE_CREATED); 277 278 if(sPath.startsWith(HarmonisePaths.PATH_USERS)) { 279 VirtualFile vfFile = this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource(); 280 if(vfFile!=null) { 281 vfFile.lock(); 282 ActionChangeUserPassword action = new ActionChangeUserPassword(); 283 action.changePassword("Set the user's password.", this.getLastContextDirectory().getVFS().getVirtualFile(sPath).getResource()); 284 } 285 } 286 } else { 287 } 288 } 289 } catch (Exception e) { 290 e.printStackTrace(System.err); 291 statusOverall.setStatusLevel(StatusData.LEVEL_ERROR); 292 } finally { 293 if(bChange) { 294 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME"); 295 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY); 296 ContextHandler.getInstance().fireContextEvent(ce); 297 } 298 StateHandler.getInstance().removeWait("NEW-FILE-ACTION"); 299 if(!sName.equals("")) { 300 StatusMessage statusMessage = new StatusMessage(ActionNewFile.ACTION_NAME, statusOverall); 301 statusMessage.setResourceTitle(sName); 302 statusMessage.setPath(sPath); 303 VFSMessageBuilder.getInstance().fireMessage( statusMessage ); 304 } 305 } 306 } 307 } 308 309 314 private String getNewName() { 315 JFrame frame = new JFrame(); 316 frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() ); 317 318 SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Resource name"); 319 dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter"); 320 dialog.show(); 321 String sName = dialog.getTextValue(); 322 323 while(!sName.equals("") && !this.checkName(sName)) { 324 dialog = new SingleTextEntryDialog(frame, "Resource name"); 325 dialog.setLabelText("Only use a-z,A-Z,_ and 0-9 in name, start with a letter"); 326 dialog.setLabelColor(Color.RED); 327 dialog.show(); 328 sName = dialog.getTextValue(); 329 } 330 331 return sName; 332 333 } 334 335 341 private boolean checkName(String sName) { 342 boolean bRetn = true; 343 String [] validChars = new String []{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","_"}; 344 String [] numbers = new String []{"0","1","2","3","4","5","6","7","8","9"}; 345 List aNumbers = Arrays.asList(numbers); 346 List aValidChars = Arrays.asList(validChars); 347 348 char[] chars = sName.toCharArray(); 349 for (int i = 0; i < chars.length; i++) { 350 char c = chars[i]; 351 if(i==0 && aNumbers.contains(new String (new char[]{c})) ) { 352 bRetn=false; 353 break; 354 } 355 if(!aValidChars.contains(new String (new char[]{c}))) { 356 bRetn=false; 357 break; 358 } 359 } 360 361 return bRetn; 362 } 363 364 367 public JMenuItem getMenuItem() { 368 JMenuItem menuItem = super.getMenuItem(); 369 menuItem.setAccelerator( KeyStroke.getKeyStroke(this.getAcceleratorKeycode(), this.getAcceleratorMask()) ); 370 371 return menuItem; 372 } 373 374 377 public String getDescription() { 378 return "Creates a new resource under the currently selected collection"; 379 } 380 381 384 public String getText() { 385 return "New ..."; 386 } 387 388 391 public String getToolTip() { 392 return this.getDescription(); 393 } 394 395 398 public Icon getIcon() { 399 return IconManager.getInstance().getIcon("16-document.gif"); 400 } 401 402 405 public int getAcceleratorKeycode() { 406 return KeyEvent.VK_N; 407 } 408 409 412 public String getMnemonic() { 413 return "N"; 414 } 415 416 419 public int getAcceleratorMask() { 420 return InputEvent.CTRL_MASK; 421 } 422 423 426 public boolean isEnabled(ContextEvent ce) { 427 if(ce.CONTEXT_TYPE==ContextType.CONTEXT_DIRS) { 429 430 renameButton(ce.getPath()); 431 432 m_bIsEnabled = super.isEnabled(ce); 433 return m_bIsEnabled; 434 } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) { 435 m_bIsEnabled = false; 436 this.setEnabled(false); 437 return m_bIsEnabled; 438 } else { 439 this.setEnabled(m_bIsEnabled); 440 return m_bIsEnabled; 441 } 442 } 443 444 449 private void renameButton(String sPath) { 450 if(!sPath.equals(this.m_sCurrentPath)) { 451 Iterator itor = this.m_namePathMapping.keySet().iterator(); 452 boolean bFound=false; 453 while(itor.hasNext()) { 454 String sTestPath = (String )itor.next(); 455 if(sPath.startsWith(sTestPath)) { 456 try { 457 this.getButton().setText( (String )this.m_namePathMapping.get(sTestPath) ); 458 this.getButton().setIcon( IconManager.getInstance().getIcon((String ) this.m_nameIconMapping.get(sTestPath)) ); 459 this.getMenuItem().setText( (String )this.m_namePathMapping.get(sTestPath) ); 460 this.getMenuItem().setIcon( IconManager.getInstance().getIcon((String ) this.m_nameIconMapping.get(sTestPath)) ); 461 } catch(Exception e) { 462 463 } 464 bFound=true; 465 } 466 } 467 468 if(!bFound) { 469 this.getButton().setText("New ..."); 470 this.getButton().setIcon( IconManager.getInstance().getIcon("16-document.gif") ); 471 this.getMenuItem().setText("New ..."); 472 this.getMenuItem().setIcon( IconManager.getInstance().getIcon("16-document.gif") ); 473 this.m_sCurrentPath = ""; 474 } else { 475 this.m_sCurrentPath = sPath; 476 } 477 } 478 } 479 480 } 481 | Popular Tags |