From ebb5dbaabbbf04d13e210271965b2ef50366cbc7 Mon Sep 17 00:00:00 2001 From: Tomas O'Connor Date: Tue, 12 Nov 2002 13:22:58 +0000 Subject: [PATCH] Initial checkin of org.openoffice.idesupport java package for shared classes supporting Office Scripting in Java in various IDEs --- .../openoffice/idesupport/LocalOffice.java | 82 +++ .../openoffice/idesupport/OfficeDocument.java | 114 +++++ .../openoffice/idesupport/SVersionRCFile.java | 138 +++++ .../idesupport/filter/AllFilesFilter.java | 20 + .../idesupport/filter/BinaryOnlyFilter.java | 30 ++ .../idesupport/filter/ExceptParcelFilter.java | 32 ++ .../idesupport/filter/FileFilter.java | 5 + .../localoffice/LocalOfficeImpl.java | 132 +++++ .../idesupport/ui/ConfigurePanel.java | 150 ++++++ .../openoffice/idesupport/ui/MethodPanel.java | 171 +++++++ .../openoffice/idesupport/ui/ScriptPanel.java | 166 ++++++ .../java/org/openoffice/idesupport/ui/add.gif | Bin 0 -> 103 bytes .../openoffice/idesupport/xml/Manifest.java | 138 +++++ .../idesupport/zip/ParcelZipper.java | 477 ++++++++++++++++++ 14 files changed, 1655 insertions(+) create mode 100644 scripting/java/org/openoffice/idesupport/LocalOffice.java create mode 100644 scripting/java/org/openoffice/idesupport/OfficeDocument.java create mode 100644 scripting/java/org/openoffice/idesupport/SVersionRCFile.java create mode 100644 scripting/java/org/openoffice/idesupport/filter/AllFilesFilter.java create mode 100644 scripting/java/org/openoffice/idesupport/filter/BinaryOnlyFilter.java create mode 100644 scripting/java/org/openoffice/idesupport/filter/ExceptParcelFilter.java create mode 100644 scripting/java/org/openoffice/idesupport/filter/FileFilter.java create mode 100644 scripting/java/org/openoffice/idesupport/localoffice/LocalOfficeImpl.java create mode 100644 scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java create mode 100644 scripting/java/org/openoffice/idesupport/ui/MethodPanel.java create mode 100644 scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java create mode 100644 scripting/java/org/openoffice/idesupport/ui/add.gif create mode 100644 scripting/java/org/openoffice/idesupport/xml/Manifest.java create mode 100644 scripting/java/org/openoffice/idesupport/zip/ParcelZipper.java diff --git a/scripting/java/org/openoffice/idesupport/LocalOffice.java b/scripting/java/org/openoffice/idesupport/LocalOffice.java new file mode 100644 index 000000000000..1d1c1db5d322 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/LocalOffice.java @@ -0,0 +1,82 @@ + +package org.openoffice.idesupport; + +import java.io.File; +import java.net.ConnectException; +import java.util.Vector; + +/** + * LocalOffice represents a connection to the local office. + * + * This class allows to get access to some scripting framework + * releated functionality of the locally running office. The + * office has to be started with options appropriate for establishing + * local connection. + * + * @author misha + */ +public class LocalOffice +{ + /** + * Creates an instance of the local office connection. + * + * @param parent is an application specific class loader. + * @param officePath is a platform specific path string + * to the office distribution. + * @param port is a communication port. + */ + public static final LocalOffice create( + ClassLoader parent, String officePath, int port) + { + Vector path = new Vector(); + path.addElement(officePath + "/program/classes/ridl.jar"); + path.addElement(officePath + "/program/classes/jurt.jar"); + path.addElement(officePath + "/program/classes/sandbox.jar"); + path.addElement(officePath + "/program/classes/unoil.jar"); + path.addElement(officePath + "/program/classes/juh.jar"); + path.addElement(System.getProperties().getProperty("netbeans.home") + + File.separator + "modules" + + File.separator + "ext" + + File.separator + "localoffice.jar"); + ClassLoader appcl = new DefaultScriptClassLoader(parent, path); + Class clazz = null; + LocalOffice office = null; + try { + clazz = appcl.loadClass( + "org.openoffice.idesupport.localoffice.LocalOfficeImpl"); + office = (LocalOffice)clazz.newInstance(); + office.connect(officePath, port); + } catch (java.lang.Exception exp) { + office = null; + } + return office; + } + + /** + * Connects to the running office. + * + * @param officePath is a platform specific path string + * to the office distribution. + * @param port is a communication port. + */ + protected void connect(String officePath, int port) + throws ConnectException + { + } + + /** + * Closes the connection to the running office. + */ + public void disconnect() + { + } + + /** + * Refresh the script storage. + * + * @param uri is an identifier of storage has to be refreshed. + */ + public void refreshStorage(String uri) + { + } +} diff --git a/scripting/java/org/openoffice/idesupport/OfficeDocument.java b/scripting/java/org/openoffice/idesupport/OfficeDocument.java new file mode 100644 index 000000000000..539393fa7bc9 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/OfficeDocument.java @@ -0,0 +1,114 @@ +package org.openoffice.idesupport; + +import java.io.*; +import java.util.zip.*; +import java.util.Enumeration; +import java.util.StringTokenizer; +import java.beans.PropertyVetoException; +import javax.naming.InvalidNameException; + +import org.openoffice.idesupport.filter.FileFilter; +import org.openoffice.idesupport.filter.BinaryOnlyFilter; +import org.openoffice.idesupport.zip.ParcelZipper; + +public class OfficeDocument +{ + public static final String PARCEL_PREFIX_DIR = "Scripts/java/"; + public static final String OFFICE_EXTENSIONS = "sxc,sxw"; + public static final String ARCHIVE_TAG = "[PARCEL_FILE]"; + + private static ParcelZipper zipper = ParcelZipper.getParcelZipper(); + private File officeFile = null; + private String parcelName = null; + private String extension = null; + + public OfficeDocument(File officeFile) throws InvalidNameException + { + this.officeFile = officeFile; + if( !checkIfOfficeDocument() ) + { + throw new InvalidNameException("This is not a valid StarOffice document."); + } + } + + private boolean checkIfOfficeDocument() + { + if( officeFile.isDirectory() ) + { + return false; + } + String tmpName = officeFile.getName(); + if( tmpName.lastIndexOf(".") == 0 ) + { + return false; + } + this.extension = tmpName.substring(tmpName.lastIndexOf(".")+1); + if( (OFFICE_EXTENSIONS.indexOf(extension)==-1) ) + { + return false; + } + this.parcelName = tmpName.substring(0,tmpName.lastIndexOf(".")); + return true; + } + + public Enumeration getParcels() + { + java.util.Vector parcelEntries = new java.util.Vector(); + try + { + ZipFile zp = new ZipFile(this.officeFile); + + for (Enumeration officeEntries = zp.entries(); officeEntries.hasMoreElements(); ) + { + ZipEntry ze = (ZipEntry)officeEntries.nextElement(); + if (ze.getName().endsWith(ParcelZipper.PARCEL_DESCRIPTOR_XML)) + { + String tmp = ze.getName(); + int end = tmp.lastIndexOf("/"); + tmp = tmp.substring(0, end); + int start = tmp.lastIndexOf("/") + 1; + + String parcelName = ARCHIVE_TAG + + ze.getName().substring(start, end); + parcelEntries.add(parcelName); + } + } + } + catch(ZipException ze) + { + ze.printStackTrace(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + } + return parcelEntries.elements(); + } + + public String getParcelNameFromEntry(String parcelName) + { + return parcelName.substring(PARCEL_PREFIX_DIR.length(), parcelName.length()-1); + } + + public String getParcelEntryFromName(String parcelName) + { + return parcelName.substring(ARCHIVE_TAG.length()) + "/"; + } + + public boolean removeParcel(String parcelName) + { + try { + ParcelZipper.getParcelZipper().unzipToZipExceptParcel(this.officeFile, getParcelEntryFromName(parcelName)); + } + catch (IOException ioe) { + ioe.printStackTrace(); + return false; + } + return true; + } + + public String unzipOneParcel(String parcelName) + { + return new String("location"); + } +} diff --git a/scripting/java/org/openoffice/idesupport/SVersionRCFile.java b/scripting/java/org/openoffice/idesupport/SVersionRCFile.java new file mode 100644 index 000000000000..3d09d974fb7a --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/SVersionRCFile.java @@ -0,0 +1,138 @@ +package org.openoffice.idesupport; + +import java.io.*; +import java.util.*; + +public class SVersionRCFile { + + public static final String DEFAULT_NAME = + System.getProperty("os.name").startsWith("Windows") == true ? + System.getProperty("user.home") + File.separator + + "Application Data" + File.separator + "sversion.ini" : + System.getProperty("user.home") + File.separator + + ".sversionrc"; + + private static final String VERSIONS_LINE = "[Versions]"; + private static final String UNOILJAR = "skip_registration" + File.separator + "unoil.jar"; + private static final String UNOPACKAGEDIR = File.separator + "user" + + File.separator + "uno_packages" + + File.separator + "cache" + + File.separator + "uno_packages"; + /* Make sure this is in LowerCase !!!!! */ + private static final String SCRIPTF = "scriptf"; + private static final String FILE_URL_PREFIX = "file://"; + private File file = null; + + public SVersionRCFile() { + this(DEFAULT_NAME); + } + + public SVersionRCFile(String name) { + file = new File(name); + } + + public Hashtable getVersions() throws IOException { + BufferedReader br; + + try { + br = new BufferedReader(new FileReader(file)); + } + catch (FileNotFoundException fnfe) { + throw new IOException(fnfe.getMessage()); + } + + Hashtable versions = load(br); + br.close(); + return versions; + } + + private Hashtable load(BufferedReader br) throws IOException { + Hashtable versions = new Hashtable(); + String s; + + while ((s = br.readLine()) != null && + (s.equals(VERSIONS_LINE)) != true); + + while ((s = br.readLine()) != null && + (s.equals("")) != true) { + StringTokenizer tokens = new StringTokenizer(s, "="); + int count = tokens.countTokens(); + + if (count != 2) + continue; + + String name = tokens.nextToken(); + String path = tokens.nextToken(); + if (path.startsWith(FILE_URL_PREFIX) == true) + path = path.substring(FILE_URL_PREFIX.length()); + + if (isValidPath(path) == true) + versions.put(name, path); + } + return versions; + } + + private boolean isValidPath(String path) { + // System.out.println("Testing: " + path); + File file = new File(path + File.separator + "program"); + if (file.exists()) + return true; + return false; + } + + public static String getPathForUnoil(String officeInstall) + { + File unopkgdir = new File(officeInstall, UNOPACKAGEDIR); + if(!unopkgdir.exists()) + { + return null; + } + File scriptf = null; + String[] listunopkg = unopkgdir.list(); + int size = listunopkg.length; + for(int i=0; i-1) + { + scriptf = new File(unopkgdir, listunopkg[i]); + } + } + if(scriptf != null) + { + File unoil = new File(scriptf, UNOILJAR); + if(unoil.exists()) + { + String path = unoil.getParent(); + path = path.substring(path.indexOf(UNOPACKAGEDIR)); + return officeInstall + path; + } + } + return null; + } + + /* public static void main(String[] args) { + SVersionRCFile ov; + Hashtable versions; + + if (args.length == 0) + ov = new SVersionRCFile(); + else + ov = new SVersionRCFile(args[0]); + + try { + versions = ov.getVersions(); + } + catch (IOException ioe) { + System.err.println("Error getting versions: " + ioe.getMessage()); + return; + } + + Enumeration enum = versions.keys(); + + while (enum.hasMoreElements()) { + String name = (String)enum.nextElement(); + System.out.println("Name: " + name + ", Path: " + + (String)versions.get(name)); + } + } */ +} diff --git a/scripting/java/org/openoffice/idesupport/filter/AllFilesFilter.java b/scripting/java/org/openoffice/idesupport/filter/AllFilesFilter.java new file mode 100644 index 000000000000..9684cbe2d710 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/filter/AllFilesFilter.java @@ -0,0 +1,20 @@ +package org.openoffice.idesupport.filter; + +public class AllFilesFilter implements FileFilter { + private static final AllFilesFilter filter = new AllFilesFilter(); + + private AllFilesFilter() { + } + + public static AllFilesFilter getInstance() { + return filter; + } + + public boolean validate(String name) { + return true; + } + + public String toString() { + return ""; + } +} diff --git a/scripting/java/org/openoffice/idesupport/filter/BinaryOnlyFilter.java b/scripting/java/org/openoffice/idesupport/filter/BinaryOnlyFilter.java new file mode 100644 index 000000000000..76d5fbea221f --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/filter/BinaryOnlyFilter.java @@ -0,0 +1,30 @@ +package org.openoffice.idesupport.filter; + +public class BinaryOnlyFilter implements FileFilter { + private static final String[] EXTENSIONS = {".class", ".jar"}; + private static final String DESCRIPTION = "Binaries Only"; + private static final BinaryOnlyFilter filter = new BinaryOnlyFilter(); + + private BinaryOnlyFilter() { + } + + public static BinaryOnlyFilter getInstance() { + return filter; + } + public boolean validate(String name) { + for (int i = 0; i < EXTENSIONS.length; i++) + if (name.endsWith(EXTENSIONS[i])) + return true; + return false; + } + + public String toString() { + StringBuffer buf = new StringBuffer(DESCRIPTION + ": "); + + for (int i = 0; i < EXTENSIONS.length - 1; i++) + buf.append("<" + EXTENSIONS[i] + "> "); + buf.append("<" + EXTENSIONS[EXTENSIONS.length - 1] + ">"); + + return buf.toString(); + } +} diff --git a/scripting/java/org/openoffice/idesupport/filter/ExceptParcelFilter.java b/scripting/java/org/openoffice/idesupport/filter/ExceptParcelFilter.java new file mode 100644 index 000000000000..0380e43630da --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/filter/ExceptParcelFilter.java @@ -0,0 +1,32 @@ +package org.openoffice.idesupport.filter; + +public class ExceptParcelFilter implements FileFilter { + private static final String DESCRIPTION = "Remove specified Parcel"; + private static final ExceptParcelFilter filter = new ExceptParcelFilter(); + private static String parcelName = null; + + private ExceptParcelFilter() { + } + + public void setParcelToRemove(String parcelName) + { + this.parcelName = parcelName; + } + + public static ExceptParcelFilter getInstance() { + return filter; + } + public boolean validate(String name) { + if (name.startsWith(this.parcelName)) + return true; + return false; + } + + public String toString() { + StringBuffer buf = new StringBuffer(DESCRIPTION + ": "); + + buf.append("<" + this.parcelName + ">"); + + return buf.toString(); + } +} diff --git a/scripting/java/org/openoffice/idesupport/filter/FileFilter.java b/scripting/java/org/openoffice/idesupport/filter/FileFilter.java new file mode 100644 index 000000000000..db2a93e962d1 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/filter/FileFilter.java @@ -0,0 +1,5 @@ +package org.openoffice.idesupport.filter; + +public interface FileFilter { + public boolean validate(String name); +} diff --git a/scripting/java/org/openoffice/idesupport/localoffice/LocalOfficeImpl.java b/scripting/java/org/openoffice/idesupport/localoffice/LocalOfficeImpl.java new file mode 100644 index 000000000000..dbf7dcff3f21 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/localoffice/LocalOfficeImpl.java @@ -0,0 +1,132 @@ + +package org.openoffice.idesupport.localoffice; + +import java.net.ConnectException; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XComponent; +import com.sun.star.bridge.XUnoUrlResolver; +import com.sun.star.beans.XPropertySet; +import com.sun.star.comp.helper.Bootstrap; +import com.sun.star.uno.XComponentContext; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.Exception; + +import drafts.com.sun.star.script.framework.storage.XScriptStorageManager; + +import org.openoffice.idesupport.LocalOffice; + +/** + * LocalOfficeImpl represents a connection to the local office. + * + * This class is an implementation of LocalOffice ane allows to + * get access to some scripting framework releated functionality + * of the locally running office. The office has to be started + * with options appropriate for establishing local connection. + * + * @author misha + */ +public final class LocalOfficeImpl + extends LocalOffice +{ + private final static String STORAGE_MRG_SINGLETON = + "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager"; + + private transient String mOfficePath; + private transient XMultiComponentFactory mComponentFactory; + private transient XComponentContext mComponentContext; + private transient XMultiServiceFactory mServiceFactory; + /** + * Constructor. + */ + public LocalOfficeImpl() + { + } + + /** + * Connects to the running office. + * + * @param officePath is a platform specific path string + * to the office distribution. + * @param port is a communication port. + */ + protected void connect(String officePath, int port) + throws ConnectException + { + mOfficePath = officePath; + try { + bootstrap(port); + } catch (java.lang.Exception ex) { + throw new ConnectException(ex.getMessage()); + } + } + + /** + * Refresh the script storage. + * + * @param uri is an identifier of storage has to be refreshed. + */ + public void refreshStorage(String uri) + { + try { + Object object = null; + object = mComponentContext.getValueByName(STORAGE_MRG_SINGLETON); + XScriptStorageManager storageMgr; + storageMgr = (XScriptStorageManager)UnoRuntime.queryInterface( + XScriptStorageManager.class, object); + storageMgr.refreshScriptStorage(uri); + } catch (java.lang.Exception ex) { +System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getMessage()); +System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getClass().getName()); + } +System.out.println("*** LocalOfficeImpl.refreshStorage: DONE"); + } + + /** + * Closes the connection to the running office. + */ + public void disconnect() + { +/* + if(mComponentFactory != null) { + XComponent comp = (XComponent)UnoRuntime.queryInterface( + XComponent.class, mComponentFactory); + comp.dispose(); + } +*/ + } + + /** + * Boot straps UNO. + * + * The office has to be started with following string: + * "-accept=socket,host=localhost,port=;urp;StarOffice.ServiceManager" + * + * @param port is a communication port. + */ + private void bootstrap(int port) + throws java.lang.Exception + { + Object object; + mComponentContext = Bootstrap.createInitialComponentContext(null); + mComponentFactory = mComponentContext.getServiceManager(); + object = mComponentFactory.createInstanceWithContext( + "com.sun.star.bridge.UnoUrlResolver", mComponentContext); + XUnoUrlResolver urlresolver; + urlresolver = (XUnoUrlResolver)UnoRuntime.queryInterface( + XUnoUrlResolver.class, object); + object = urlresolver.resolve( + "uno:socket,host=localhost,port=" + + port + + ";urp;StarOffice.ServiceManager"); + mComponentFactory = (XMultiComponentFactory)UnoRuntime.queryInterface( + XMultiComponentFactory.class, object); + XPropertySet factoryProps; + factoryProps = (XPropertySet)UnoRuntime.queryInterface( + XPropertySet.class, mComponentFactory); + object = factoryProps.getPropertyValue("DefaultContext"); + mComponentContext = (XComponentContext)UnoRuntime.queryInterface( + XComponentContext.class, object); + } +} diff --git a/scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java b/scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java new file mode 100644 index 000000000000..67be8db96764 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java @@ -0,0 +1,150 @@ +package org.openoffice.idesupport.ui; + +import java.io.File; +import java.util.Vector; +import java.util.Enumeration; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JButton; +import javax.swing.AbstractButton; +import javax.swing.ImageIcon; +import javax.swing.border.LineBorder; + +import java.awt.BorderLayout; +import java.awt.GridBagLayout; +import java.awt.GridBagConstraints; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import org.w3c.dom.Document; + +import org.openoffice.idesupport.xml.ParcelDescriptor; +import org.openoffice.idesupport.zip.ParcelZipper; + +public class ConfigurePanel extends JPanel { + + private File basedir; + private Vector classpath; + + private MethodPanel methodPanel; + private ScriptPanel scriptPanel; + private ParcelDescriptor descriptor = ParcelDescriptor.getParcelDescriptor(); + + public ConfigurePanel(String basedir, Vector classpath, Document doc) { + this.basedir = new File(basedir); + this.classpath = classpath; + initUI(doc); + } + + public Document getConfiguration() throws Exception { + Enumeration scripts = scriptPanel.getScriptEntries(); + return descriptor.generate(scripts); + } + + private void initUI(Document doc) { + JPanel leftPanel = new JPanel(); + JPanel methodButtons = initMethodButtons(); + methodPanel = new MethodPanel(basedir, classpath); + leftPanel.setLayout(new BorderLayout()); + leftPanel.add(methodPanel, BorderLayout.CENTER); + + JPanel rightPanel = new JPanel(); + JPanel scriptButtons = initScriptButtons(); + scriptPanel = new ScriptPanel(descriptor.parse(doc)); + rightPanel.setLayout(new BorderLayout()); + rightPanel.add(scriptPanel, BorderLayout.CENTER); + rightPanel.add(scriptButtons, BorderLayout.SOUTH); + + setLayout(new GridBagLayout()); + setPreferredSize(new java.awt.Dimension(700, 300)); + setBorder(LineBorder.createBlackLineBorder()); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.gridy = 0; + gbc.fill = java.awt.GridBagConstraints.BOTH; + gbc.ipadx = 40; + gbc.anchor = java.awt.GridBagConstraints.WEST; + gbc.insets = new Insets(10, 5, 5, 5); + gbc.weightx = 0.75; + add(leftPanel, gbc); + + gbc = new java.awt.GridBagConstraints(); + gbc.gridx = 1; + gbc.gridy = 0; + add(methodButtons, gbc); + + gbc = new java.awt.GridBagConstraints(); + gbc.gridx = 2; + gbc.gridy = 0; + gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER; + gbc.fill = java.awt.GridBagConstraints.BOTH; + gbc.anchor = java.awt.GridBagConstraints.EAST; + gbc.insets = new Insets(10, 5, 5, 5); + gbc.weightx = 1.0; + gbc.weighty = 1.0; + add(rightPanel, gbc); + } + + private JPanel initMethodButtons() { + JPanel panel = new JPanel(); + panel.setLayout(new GridBagLayout()); + ImageIcon icon = new ImageIcon(getClass().getResource("/org/openoffice/idesupport/ui/add.gif")); + JButton addButton = new JButton("Add", icon); + addButton.setHorizontalTextPosition(AbstractButton.LEFT); + + addButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + scriptPanel.addScriptEntries(methodPanel.getSelectedEntries()); + } + } + ); + + GridBagConstraints gbc = new java.awt.GridBagConstraints(); + gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER; + gbc.fill = java.awt.GridBagConstraints.HORIZONTAL; + gbc.insets = new Insets(5, 5, 5, 5); + panel.add(addButton, gbc); + + JPanel dummyPanel = new JPanel(); + gbc = new java.awt.GridBagConstraints(); + gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER; + gbc.gridheight = java.awt.GridBagConstraints.REMAINDER; + gbc.fill = java.awt.GridBagConstraints.BOTH; + gbc.weightx = 1.0; + gbc.weighty = 1.0; + panel.add(dummyPanel, gbc); + + return panel; + } + + private JPanel initScriptButtons() { + JPanel panel = new JPanel(); + JButton removeButton = new JButton("Remove"); + JButton removeAllButton = new JButton("Remove All"); + + removeButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + scriptPanel.removeSelectedRows(); + } + } + ); + + removeAllButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + scriptPanel.removeAllRows(); + } + } + ); + + panel.add(removeButton); + panel.add(removeAllButton); + + return panel; + } +} diff --git a/scripting/java/org/openoffice/idesupport/ui/MethodPanel.java b/scripting/java/org/openoffice/idesupport/ui/MethodPanel.java new file mode 100644 index 000000000000..348eeec04ec3 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/ui/MethodPanel.java @@ -0,0 +1,171 @@ +package org.openoffice.idesupport.ui; + +import java.io.File; +import java.util.Vector; +import java.util.ArrayList; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JList; +import javax.swing.JLabel; +import java.awt.BorderLayout; + +import org.openoffice.idesupport.ScriptEntry; +import org.openoffice.idesupport.DefaultScriptClassLoader; +import org.openoffice.idesupport.zip.ParcelZipper; + +public class MethodPanel extends JPanel { + + private File basedir; + private Vector classpath; + private final static String FIRST_PARAM = "drafts.com.sun.star.script.framework.XScriptContext"; + + private JList list; + private Vector values = new Vector(11); + + public MethodPanel(File basedir, Vector classpath) { + this.basedir = basedir; + this.classpath = classpath; + initValues(); + initUI(); + } + + public ScriptEntry[] getSelectedEntries() { + Object[] selections = list.getSelectedValues(); + ScriptEntry[] entries = new ScriptEntry[selections.length]; + + for (int i = 0; i < selections.length; i++) { + entries[i] = (ScriptEntry)selections[i]; + } + + return entries; + } + + private void initUI() { + JLabel label = new JLabel("Available Methods:"); + list = new JList(values); + JScrollPane pane = new JScrollPane(list); + label.setLabelFor(pane); + + BorderLayout layout = new BorderLayout(); + setLayout(layout); + layout.setVgap(5); + + add(label, BorderLayout.NORTH); + add(pane, BorderLayout.CENTER); + } + + private void initValues() { + String[] classNames; + String parcelName; + + if (basedir == null || basedir.exists() == false || + basedir.isDirectory() == false) + return; + + classNames = findClassNames(); + if (classNames == null || classNames.length == 0) + return; + + parcelName = basedir.getName(); + if (parcelName.equals(ParcelZipper.CONTENTS_DIRNAME)) + parcelName = basedir.getParentFile().getName(); + + DefaultScriptClassLoader classloader = + new DefaultScriptClassLoader(classpath); + + for (int i = 0; i < classNames.length; i++) + { + try + { + Class clazz = classloader.loadClass(classNames[i]); + Method[] methods = clazz.getDeclaredMethods(); + for (int k = 0; k < methods.length; k++) + { + if (Modifier.isPublic(methods[k].getModifiers())) + { + Class[] params = methods[k].getParameterTypes(); + if(params.length > 0) + { + if(params[0].getName().equals(FIRST_PARAM)) + { + ScriptEntry entry = + new ScriptEntry(classNames[i] + "." + methods[k].getName(), + parcelName); + values.addElement(entry); + } + } + } + } + } + catch (ClassNotFoundException e) + { + System.err.println("Class Not Found Exception..."); + continue; + } + catch (NoClassDefFoundError nc) + { + System.err.println("No Class Definition Found..."); + continue; + } + } + } + + private ArrayList findFiles(File basedir, String suffix) { + ArrayList result = new ArrayList(); + File[] children = basedir.listFiles(); + + for (int i = 0; i < children.length; i++) { + if (children[i].isDirectory()) + result.addAll(findFiles(children[i], suffix)); + else if (children[i].getName().endsWith(suffix)) + result.add(children[i]); + } + return result; + } + + private String[] findClassNames() + { + ArrayList classFiles = findFiles(basedir, ".class"); + if(classFiles == null || classFiles.size() == 0) + return null; + + ArrayList javaFiles = findFiles(basedir, ".java"); + if(javaFiles == null || javaFiles.size() == 0) + return null; + + ArrayList result = new ArrayList(); + for (int i = 0; i < classFiles.size(); i++) + { + File classFile = (File)classFiles.get(i); + String className = classFile.getName(); + className = className.substring(0, className.lastIndexOf(".class")); + boolean finished = false; + + + for (int j = 0; j < javaFiles.size() && finished == false; j++) + { + File javaFile = (File)javaFiles.get(j); + String javaName = javaFile.getName(); + javaName = javaName.substring(0, javaName.lastIndexOf(".java")); + + + if (javaName.equals(className)) + { + String path = classFile.getAbsolutePath(); + path = path.substring(basedir.getAbsolutePath().length() + 1); + path = path.replace(File.separatorChar, '.'); + path = path.substring(0, path.lastIndexOf(".class")); + + result.add(path); + javaFiles.remove(j); + finished = true; + } + } + } + return (String[])result.toArray(new String[0]); + } +} diff --git a/scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java b/scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java new file mode 100644 index 000000000000..2b663356cc01 --- /dev/null +++ b/scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java @@ -0,0 +1,166 @@ +package org.openoffice.idesupport.ui; + +import java.io.File; +import java.util.Vector; +import java.util.Enumeration; + +import java.awt.BorderLayout; +import java.awt.event.FocusEvent; +import java.awt.event.FocusAdapter; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JLabel; +import javax.swing.JTextField; +import javax.swing.JTable; +import javax.swing.DefaultCellEditor; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableColumn; +import javax.swing.table.AbstractTableModel; + +import org.openoffice.idesupport.ScriptEntry; + +public class ScriptPanel extends JPanel { + private ScriptTableModel model; + private JTable table; + private File descriptor; + + public ScriptPanel(ScriptEntry[] scripts) { + model = new ScriptTableModel(scripts); + initUI(); + } + + public void addScriptEntries(ScriptEntry[] entries) { + for (int i = 0; i < entries.length; i++) { + ScriptEntry entry = new ScriptEntry(entries[i].getLanguageName(), + entries[i].getLocation()); + entry.setLogicalName(entries[i].getLanguageName()); + model.add(entry); + } + } + + public void removeSelectedRows() { + int[] selections = table.getSelectedRows(); + + for (int i = selections.length - 1; i >= 0; i--) { + model.remove(selections[i]); + } + } + + public void removeAllRows() { + model.removeAll(); + } + + public Enumeration getScriptEntries() { + return model.getScriptEntries(); + } + + private void initUI() { + table = new JTable(model); + TableColumn column = table.getColumnModel().getColumn(1); + column.setCellEditor(new DefaultCellEditor(new JTextField())); + + table.addFocusListener(new FocusAdapter() { + public void focusLost(FocusEvent evt) { + tableFocusLost(evt); + } + }); + + JScrollPane pane = new JScrollPane(table); + JLabel label = new JLabel("Scripts:"); + label.setLabelFor(pane); + + BorderLayout layout = new BorderLayout(); + setLayout(layout); + layout.setVgap(5); + add(label, BorderLayout.NORTH); + add(pane, BorderLayout.CENTER); + } + + private void tableFocusLost(FocusEvent evt) { + TableCellEditor editor = table.getCellEditor(); + if (editor != null) { + Object value = editor.getCellEditorValue(); + if (value != null) + model.setValueAt(value, + table.getEditingRow(), table.getEditingColumn()); + } + } + + private class ScriptTableModel extends AbstractTableModel { + final String[] columnNames = {"Exported Method", + "Script Name"}; + + private Vector scripts; + private int nextRow; + + public ScriptTableModel(ScriptEntry[] entries) { + scripts = new Vector(entries.length + 11); + for (int i = 0; i < entries.length; i++) { + scripts.addElement(entries[i]); + } + nextRow = entries.length; + } + + public int getColumnCount() { + return columnNames.length; + } + + public int getRowCount() { + return scripts.size(); + } + + public String getColumnName(int col) { + return columnNames[col]; + } + + public void add(ScriptEntry entry) { + scripts.addElement(entry); + fireTableRowsInserted(nextRow, nextRow); + nextRow++; + } + + public void remove(int row) { + scripts.removeElementAt(row); + fireTableRowsDeleted(row, row); + nextRow--; + } + + public void removeAll() { + scripts.removeAllElements(); + fireTableRowsDeleted(0, nextRow); + nextRow = 0; + } + + public Enumeration getScriptEntries() { + return scripts.elements(); + } + + public Object getValueAt(int row, int col) { + String result = ""; + ScriptEntry entry; + + entry = (ScriptEntry)scripts.elementAt(row); + + if (col == 0) + result = entry.getLanguageName(); + else if (col == 1) + result = entry.getLogicalName(); + + return result; + } + + public boolean isCellEditable(int row, int col) { + if (col == 0) + return false; + else + return true; + } + + public void setValueAt(Object value, int row, int col) { + ScriptEntry entry = (ScriptEntry)scripts.elementAt(row); + entry.setLogicalName((String)value); + fireTableCellUpdated(row, col); + } + } +} diff --git a/scripting/java/org/openoffice/idesupport/ui/add.gif b/scripting/java/org/openoffice/idesupport/ui/add.gif new file mode 100644 index 0000000000000000000000000000000000000000..e47c986ccf1f374ac038d3ebcfd6a5b883c9c6dd GIT binary patch literal 103 zcmZ?wbhEHb6krfwSoELaz<~n{3=H$;ojG&n*RNl{L_|awz(DaQ3nPf310q0b9hh}E zcHLQ*$`g2^xhc-=*S`dpjOdA>39GDJ14ObeOpG__`5@{srF5&_{j47z3=Gx)