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.IOException; 27 import java.io.File; 28 import java.beans.PropertyVetoException; 29 import java.util.Enumeration; 30 import java.util.Set; 31 import java.util.HashSet; 32 import java.util.Iterator; 33 import javax.swing.event.ChangeListener; 34 import javax.swing.event.ChangeEvent; 35 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.filesystems.FileChangeListener; 40 import org.openide.filesystems.FileEvent; 41 import org.openide.filesystems.FileAttributeEvent; 42 import org.openide.filesystems.FileRenameEvent; 43 import org.openide.cookies.OpenCookie; 44 45 import org.openoffice.idesupport.OfficeDocument; 46 47 import org.openoffice.netbeans.modules.office.options.OfficeSettings; 48 import org.openoffice.netbeans.modules.office.loader.OfficeDocumentDataObject; 49 import org.openoffice.netbeans.modules.office.utils.ZipMounter; 50 import org.openoffice.netbeans.modules.office.utils.ManifestParser; 51 52 public class OfficeDocumentSupport implements OfficeDocumentCookie, OpenCookie, FileChangeListener 53 { 54 protected OfficeDocumentDataObject dataObj; 55 private boolean isMounted = false; 56 private OfficeDocument document; 57 private Set listeners; 58 OfficeDocumentSupport(OfficeDocumentDataObject dataObj)59 public OfficeDocumentSupport(OfficeDocumentDataObject dataObj) { 60 this.dataObj = dataObj; 61 FileObject fo = dataObj.getPrimaryFile(); 62 try { 63 this.document = new OfficeDocument(FileUtil.toFile(fo)); 64 } 65 catch (Exception e) { 66 e.printStackTrace(); 67 } 68 fo.addFileChangeListener(this); 69 } 70 mount()71 public void mount() { 72 File file = FileUtil.toFile(dataObj.getPrimaryFile()); 73 74 try { 75 ZipMounter.getZipMounter().mountZipFile(file); 76 isMounted = true; 77 } 78 catch (IOException ioe) { 79 ErrorManager.getDefault().notify(ioe); 80 } 81 catch (PropertyVetoException pve) { 82 ErrorManager.getDefault().notify(pve); 83 } 84 } 85 open()86 public void open () { 87 File file = FileUtil.toFile(dataObj.getPrimaryFile()); 88 89 OfficeSettings settings = OfficeSettings.getDefault(); 90 File soffice = new File(settings.getOfficeDirectory().getPath( 91 File.separator + "soffice")); 92 93 try { 94 Process p = Runtime.getRuntime ().exec (new String[] { 95 soffice.getAbsolutePath(), file.getAbsolutePath () 96 }); 97 } catch (IOException ioe) { 98 ErrorManager.getDefault().notify(ioe); 99 } 100 } 101 getParcels()102 public Enumeration getParcels() { 103 return document.getParcels(); 104 } 105 removeParcel(String name)106 public void removeParcel(String name) { 107 document.removeParcel(name); 108 dataObj.getPrimaryFile().refresh(true); 109 } 110 addChangeListener(ChangeListener cl)111 public void addChangeListener(ChangeListener cl) { 112 if (listeners == null) 113 listeners = new HashSet(); 114 115 listeners.add(cl); 116 } 117 removeChangeListener(ChangeListener cl)118 public void removeChangeListener(ChangeListener cl) { 119 if (listeners == null) 120 return; 121 122 listeners.remove(cl); 123 } 124 fileChanged(FileEvent fe)125 public void fileChanged(FileEvent fe) { 126 if (listeners != null) { 127 Iterator iter = listeners.iterator(); 128 129 while (iter.hasNext()) 130 ((ChangeListener)iter.next()).stateChanged(new ChangeEvent(this)); 131 } 132 } 133 fileAttributeChanged(FileAttributeEvent fe)134 public void fileAttributeChanged(FileAttributeEvent fe) {} fileDataCreated(FileEvent fe)135 public void fileDataCreated(FileEvent fe) {} fileDeleted(FileEvent fe)136 public void fileDeleted(FileEvent fe) {} fileFolderCreated(FileEvent fe)137 public void fileFolderCreated(FileEvent fe) {} fileRenamed(FileRenameEvent fe)138 public void fileRenamed(FileRenameEvent fe) {} 139 } 140