1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.netbeans.modules.office.actions; 25 26 import java.io.File; 27 import java.io.IOException; 28 import java.util.List; 29 import java.util.ArrayList; 30 31 import javax.swing.JMenuItem; 32 import javax.swing.JFileChooser; 33 import javax.swing.filechooser.FileFilter; 34 import javax.swing.event.ChangeEvent; 35 import javax.swing.event.ChangeListener; 36 import java.awt.event.ActionListener; 37 import java.awt.event.ActionEvent; 38 39 import org.openide.TopManager; 40 import org.openide.NotifyDescriptor; 41 import org.openide.awt.Actions; 42 import org.openide.nodes.Node; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 import org.openide.util.RequestProcessor; 46 import org.openide.util.actions.*; 47 import org.openide.awt.JMenuPlus; 48 49 import org.openoffice.idesupport.SVersionRCFile; 50 import org.openoffice.idesupport.OfficeInstallation; 51 import org.openoffice.idesupport.zip.ParcelZipper; 52 import org.openoffice.idesupport.LocalOffice; 53 54 import org.openoffice.netbeans.modules.office.utils.NagDialog; 55 import org.openoffice.netbeans.modules.office.options.OfficeSettings; 56 57 public class DeployParcelAction extends CookieAction implements Presenter.Popup { 58 59 private static final String BROWSE_LABEL = "Office Document..."; 60 private static final String DEPLOY_LABEL = "Deploy To"; 61 getName()62 public String getName () { 63 return DEPLOY_LABEL; 64 } 65 getHelpCtx()66 public HelpCtx getHelpCtx () { 67 return HelpCtx.DEFAULT_HELP; 68 } 69 getPopupPresenter()70 public JMenuItem getPopupPresenter() { 71 JMenuPlus menu = new JMenuPlus(DEPLOY_LABEL); 72 JMenuItem item, user, share; 73 final OfficeInstallation oi = OfficeSettings.getDefault().getOfficeDirectory(); 74 75 ActionListener listener = new ActionListener() { 76 public void actionPerformed(ActionEvent e) { 77 JMenuItem choice = (JMenuItem)e.getSource(); 78 String label = choice.getText(); 79 80 Node[] nodes = getActivatedNodes(); 81 final ParcelCookie parcelCookie = 82 (ParcelCookie)nodes[0].getCookie(ParcelCookie.class); 83 84 File target = new File(oi.getPath(File.separator + label + 85 File.separator + "Scripts")); 86 87 File langdir = new File(target, parcelCookie.getLanguage()); 88 89 if (!langdir.exists()) { 90 boolean response = askIfCreateDirectory(langdir); 91 if (response == false) { 92 return; 93 } 94 } 95 96 deploy(target); 97 } 98 }; 99 100 user = new JMenuItem("user"); 101 user.addActionListener(listener); 102 103 share = new JMenuItem("share"); 104 share.addActionListener(listener); 105 106 item = new JMenuPlus(oi.getName()); 107 item.add(user); 108 item.add(share); 109 menu.add(item); 110 111 menu.addSeparator(); 112 item = new JMenuItem(BROWSE_LABEL); 113 item.addActionListener(new ActionListener() { 114 public void actionPerformed(ActionEvent e) { 115 File target = getTargetFile(); 116 if (target == null) 117 return; 118 deploy(target); 119 } 120 }); 121 menu.add(item); 122 123 return menu; 124 } 125 mode()126 protected int mode () { 127 return MODE_ONE; 128 } 129 cookieClasses()130 protected Class[] cookieClasses () { 131 return new Class[] { ParcelCookie.class }; 132 } 133 performAction(Node[] activatedNodes)134 protected void performAction (Node[] activatedNodes) { 135 // do nothing, should not happen 136 } 137 deploy(final File target)138 private void deploy(final File target) { 139 Node[] nodes = getActivatedNodes(); 140 final ParcelCookie parcelCookie = 141 (ParcelCookie)nodes[0].getCookie(ParcelCookie.class); 142 143 RequestProcessor.getDefault().post(new Runnable() { 144 public void run() { 145 boolean result = parcelCookie.deploy(target); 146 147 if (result == true && target.isDirectory()) { 148 showNagDialog(); 149 // refreshOffice((String)versions.get(label)); 150 } 151 } 152 }); 153 } 154 askIfCreateDirectory(File directory)155 private boolean askIfCreateDirectory(File directory) { 156 String message = directory.getAbsolutePath() + " does not exist. " + 157 "Do you want to create it now?"; 158 159 NotifyDescriptor d = new NotifyDescriptor.Confirmation( 160 message, NotifyDescriptor.OK_CANCEL_OPTION); 161 TopManager.getDefault().notify(d); 162 163 if (d.getValue() == NotifyDescriptor.CANCEL_OPTION) 164 return false; 165 166 boolean result; 167 try { 168 result = directory.mkdirs(); 169 } 170 catch (SecurityException se) { 171 result = false; 172 } 173 174 if (result == false) { 175 String tmp = "Error creating: " + directory.getAbsolutePath(); 176 NotifyDescriptor d2 = new NotifyDescriptor.Message( 177 tmp, NotifyDescriptor.ERROR_MESSAGE); 178 TopManager.getDefault().notify(d2); 179 } 180 return result; 181 } 182 refreshOffice(String path)183 private void refreshOffice(String path) { 184 ClassLoader syscl = TopManager.getDefault().currentClassLoader(); 185 LocalOffice office = LocalOffice.create(syscl, path, 8100); 186 office.refreshStorage("file://" + path + "/program/../user"); 187 office.disconnect(); 188 } 189 showNagDialog()190 private void showNagDialog() { 191 String message = "If you currently have Office running you will " + 192 "need to click on the Tools/Scripting Add-on's/Refresh All Scripts " + 193 " menu item in Office so that the scripts in this parcel can be detected."; 194 195 OfficeSettings settings = OfficeSettings.getDefault(); 196 197 if (settings.getWarnAfterDirDeploy() == true) { 198 NagDialog warning = NagDialog.createInformationDialog( 199 message, "Show this message in future", true); 200 201 warning.show(); 202 203 if (warning.getState() == false) 204 settings.setWarnAfterDirDeploy(false); 205 } 206 } 207 getTargetFile()208 private File getTargetFile() { 209 File target = null; 210 211 JFileChooser chooser = new JFileChooser(); 212 chooser.setDialogTitle("Deploy Parcel To Office Document"); 213 chooser.setApproveButtonText("Deploy"); 214 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 215 chooser.setFileFilter(new FileFilter() { 216 public boolean accept(File file) { 217 if (file.isDirectory() || 218 file.getName().endsWith(".sxw") || 219 file.getName().endsWith(".sxc") || 220 file.getName().endsWith(".sxd") || 221 file.getName().endsWith(".sxi")) 222 return true; 223 return false; 224 } 225 226 public String getDescription() { 227 return "Office Documents"; 228 } 229 }); 230 231 int result = chooser.showDialog(null, null); 232 233 if (result == JFileChooser.APPROVE_OPTION) { 234 target = chooser.getSelectedFile(); 235 } 236 return target; 237 } 238 } 239