1 19 package org.openharmonise.him.actions.file; 20 21 import java.awt.event.*; 22 import java.util.*; 23 24 import javax.swing.*; 25 26 import org.openharmonise.him.actions.*; 27 import org.openharmonise.him.actions.rules.*; 28 import org.openharmonise.him.configuration.*; 29 import org.openharmonise.him.context.StateHandler; 30 import org.openharmonise.him.window.messages.builders.*; 31 import org.openharmonise.him.window.session.*; 32 import org.openharmonise.swing.*; 33 import org.openharmonise.vfs.*; 34 import org.openharmonise.vfs.context.*; 35 import org.openharmonise.vfs.gui.*; 36 import org.openharmonise.vfs.status.*; 37 38 39 46 public class ActionRename extends AbstractHIMAction implements HIMAction { 47 48 public static String ACTION_NAME = "RENAME"; 49 50 53 public ActionRename() { 54 super(); 55 this.setup(); 56 } 57 58 61 public ActionRename(VirtualFile vfFile) { 62 super(vfFile); 63 this.setup(); 64 } 65 66 70 private void setup() { 71 RuleGroup andGroup = new RuleGroup(); 72 andGroup.setGroupType(RuleGroup.GROUPTYPE_AND); 73 IsPublishedRule pubRule = new IsPublishedRule(); 74 andGroup.addEnableRule(pubRule); 75 PathRule pathRule = new PathRule("/webdav/Archive/"); 76 pathRule.setResultComparator(false); 77 andGroup.addEnableRule(pathRule); 78 IsHistoricalRule histRule = new IsHistoricalRule(); 79 histRule.setResultComparator(false); 80 andGroup.addEnableRule(histRule); 81 SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_MOVE); 82 andGroup.addEnableRule(secRule); 83 this.addEnableRule(andGroup); 84 } 85 86 89 public void actionPerformed(ActionEvent arg0) { 90 boolean bChange = (ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY")!=null && ConfigStore.getInstance().getPropertyValue("FILENAME_DISPLAY").equals("DISPLAYNAME")); 91 92 StatusData statusOverall = new VFSStatus(); 93 94 95 if(bChange) { 96 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "FILENAME"); 97 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY); 98 ContextHandler.getInstance().fireContextEvent(ce); 99 } 100 101 VirtualFile vfFile = this.getLastContextFile(); 102 if(vfFile.isVersionable() && vfFile.getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null) { 103 vfFile = vfFile.getVFS().getVirtualFile( ((VersionedVirtualFile)vfFile).getLiveVersionPath() ).getResource(); 104 } 105 106 String sOriginalName = vfFile.getFileName(); 107 108 String sNewName = this.getNewName(sOriginalName); 109 110 StateHandler.getInstance().addWait("RENAME-ACTION"); 111 try { 112 if(!sNewName.equals("")) { 113 StatusData status = vfFile.rename(sNewName); 114 statusOverall.addStatusData(status); 115 if( status.isOK() ) { 116 super.fireSessionEvent("Renamed", vfFile.getVFS(), vfFile.getFullPath(), SessionEventData.RESOURCE_RENAMED); 117 } else { 118 } 119 } 120 } catch (Exception e) { 121 e.printStackTrace(System.err); 122 statusOverall.setStatusLevel(StatusData.LEVEL_ERROR); 123 } finally { 124 if(bChange) { 125 ConfigStore.getInstance().setProperty("FILENAME_DISPLAY", "DISPLAYNAME"); 126 ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILENAME_DISPLAY); 127 ContextHandler.getInstance().fireContextEvent(ce); 128 } 129 VFSMessageBuilder.getInstance().fireMessage(ActionRename.ACTION_NAME, statusOverall, sOriginalName, sNewName); 130 StateHandler.getInstance().removeWait("RENAME-ACTION"); 131 } 132 } 133 134 140 private String getNewName(String sOriginalName) { 141 JFrame frame = new JFrame(); 142 frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() ); 143 144 SingleTextEntryDialog dialog = new SingleTextEntryDialog(frame, "Rename"); 145 dialog.setTextValue(sOriginalName); 146 dialog.setLabelText("Only use a-z, A-Z and 0-9 in name."); 147 dialog.show(); 148 String sName = dialog.getTextValue(); 149 150 while(!sName.equals("") && !this.checkName(sName)) { 151 dialog = new SingleTextEntryDialog(frame, "Rename"); 152 dialog.setTextValue(sName); 153 dialog.setLabelText("Only use a-z, A-Z and 0-9 in name."); 154 dialog.show(); 155 sName = dialog.getTextValue(); 156 } 157 158 return sName; 159 160 } 161 162 168 private boolean checkName(String sName) { 169 boolean bRetn = true; 170 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","0"}; 171 List aValidChars = Arrays.asList(validChars); 172 173 char[] chars = sName.toCharArray(); 174 for (int i = 0; i < chars.length; i++) { 175 char c = chars[i]; 176 if(!aValidChars.contains(new String (new char[]{c}))) { 177 bRetn=false; 178 break; 179 } 180 } 181 182 return bRetn; 183 } 184 185 188 public String getDescription() { 189 return "Renames the currently selected resource"; 190 } 191 192 195 public String getText() { 196 return "Rename..."; 197 } 198 199 202 public String getToolTip() { 203 return this.getDescription(); 204 } 205 206 209 public Icon getIcon() { 210 return IconManager.getInstance().getIcon("16-blank.gif"); 211 } 212 213 216 public int getAcceleratorKeycode() { 217 return 0; 218 } 219 220 223 public String getMnemonic() { 224 return "R"; 225 } 226 227 230 public int getAcceleratorMask() { 231 return InputEvent.CTRL_MASK; 232 } 233 234 } 235 | Popular Tags |