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