diff --git a/source/org/libreoffice/example/comp/StarterProjectImpl.java b/source/org/libreoffice/example/comp/StarterProjectImpl.java index 912c44c..de058c8 100644 --- a/source/org/libreoffice/example/comp/StarterProjectImpl.java +++ b/source/org/libreoffice/example/comp/StarterProjectImpl.java @@ -1,11 +1,29 @@ package org.libreoffice.example.comp; +import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; + +import pro.litvinovg.libreoffice.metadata.Document; +import pro.litvinovg.libreoffice.metadata.views.EditorGUI; + import com.sun.star.lib.uno.helper.Factory; +import java.util.Arrays; +import java.util.Comparator; + import org.libreoffice.example.dialog.ActionOneDialog; import org.libreoffice.example.helper.DialogHelper; +import com.sun.star.beans.Property; +import com.sun.star.beans.XPropertyContainer; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertySetInfo; +import com.sun.star.document.XDocumentProperties; +import com.sun.star.document.XDocumentPropertiesSupplier; +import com.sun.star.frame.XDesktop; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.lang.XSingleComponentFactory; import com.sun.star.registry.XRegistryKey; import com.sun.star.lib.uno.helper.WeakBase; @@ -15,16 +33,20 @@ public final class StarterProjectImpl extends WeakBase implements com.sun.star.lang.XServiceInfo, com.sun.star.task.XJobExecutor { - private final XComponentContext m_xContext; + private final XComponentContext context; + private XDesktop xDesktop; + private XMultiComponentFactory multiComponentFactory; + private XComponent currentDocument; + private XDocumentProperties documentProperties; + private XDocumentPropertiesSupplier documentPropertiesSupplier; private static final String m_implementationName = StarterProjectImpl.class.getName(); - private static final String[] m_serviceNames = { - "org.libreoffice.example.StarterProject" }; + private static final String[] m_serviceNames = { "org.libreoffice.example.StarterProject" }; - public StarterProjectImpl( XComponentContext context ) - { - m_xContext = context; - }; + public StarterProjectImpl(XComponentContext componentContext) { + context = componentContext; + + }; public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) { XSingleComponentFactory xFactory = null; @@ -64,13 +86,21 @@ public final class StarterProjectImpl extends WeakBase { switch (action) { case "actionOne": - ActionOneDialog actionOneDialog = new ActionOneDialog(m_xContext); - actionOneDialog.show(); + //ActionOneDialog actionOneDialog = new ActionOneDialog(m_xContext); + //actionOneDialog.show(); + + Document doc = new Document(context); + EditorGUI.runGUI(doc); break; default: - DialogHelper.showErrorMessage(m_xContext, null, "Unknown action: " + action); + DialogHelper.showErrorMessage(context, null, "Unknown action: " + action); } } + + + + + } diff --git a/source/pro/litvinovg/libreoffice/metadata/CustomDocumentProperty.java b/source/pro/litvinovg/libreoffice/metadata/CustomDocumentProperty.java new file mode 100644 index 0000000..b2efd3b --- /dev/null +++ b/source/pro/litvinovg/libreoffice/metadata/CustomDocumentProperty.java @@ -0,0 +1,21 @@ +package pro.litvinovg.libreoffice.metadata; + +public class CustomDocumentProperty { + private String name; + private String value; + private String type; + public CustomDocumentProperty(String name, String value, String type) { + this.name = name; + this.value = value; + this.type = type; + } + public String getType() { + return type; + } + public String getName() { + return name; + } + public String getValue() { + return value; + } +} diff --git a/source/pro/litvinovg/libreoffice/metadata/Document.java b/source/pro/litvinovg/libreoffice/metadata/Document.java new file mode 100644 index 0000000..ab9cc25 --- /dev/null +++ b/source/pro/litvinovg/libreoffice/metadata/Document.java @@ -0,0 +1,180 @@ +package pro.litvinovg.libreoffice.metadata; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import org.libreoffice.example.comp.MetadataInaccessableException; + +import com.sun.org.apache.bcel.internal.generic.NEW; +import com.sun.star.beans.Property; +import com.sun.star.beans.XPropertyContainer; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertySetInfo; +import com.sun.star.document.XDocumentProperties; +import com.sun.star.document.XDocumentPropertiesSupplier; +import com.sun.star.frame.XDesktop; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class Document { + + private static final String DOC_AUTHOR = "Document author"; + private static final String DOC_TITLE = "Document title"; + private static final String DOC_SUBJECT = "Document subject"; + private static final String DOC_DESCRIPTION = "Document description"; + private static final String DOC_KEYWORDS = "Document keywords"; + + + private XComponentContext context; + private XDesktop xDesktop; + private XMultiComponentFactory multiComponentFactory; + private XComponent currentDocument; + private XDocumentProperties documentProperties; + private XDocumentPropertiesSupplier documentPropertiesSupplier; + + public Document(XComponentContext componentContext) { + context = componentContext; + multiComponentFactory = context.getServiceManager(); + try { + Object oDesktop = multiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", context); + xDesktop = UnoRuntime.queryInterface(XDesktop.class, oDesktop); + getCurrentDocument(); + } catch (Exception e) { + System.out.println("xDesktop inaccessible. Can not proceed."); + e.printStackTrace(); + System.exit(1); + } + logProperties(); + } + + private void getCurrentDocument() throws MetadataInaccessableException { + currentDocument = xDesktop.getCurrentComponent(); + if (currentDocument == null) { + throw new MetadataInaccessableException("Could not access current document."); + } + } + + public void logProperties() { + logDocumentProperties(); + logDocumentCustomProperties(); + } + + public ArrayList getDocumentCustomProperties() { + ArrayList customProps = new ArrayList(); + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties(); + XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, userDifinedProperties); + if (propertySet != null) { + XPropertySetInfo propertySetInfo = propertySet.getPropertySetInfo(); + if (propertySetInfo != null) { + Property[] props = propertySetInfo.getProperties(); + Arrays.sort(props, new Comparator() { + public int compare(Property p1, Property p2) { + return (p1.Name).compareTo(p2.Name); + } + }); + + for (Property prop : props) { + Object propValue = getProperty(propertySet, prop.Name); + if (prop.Type.getTypeName().equals("string")){ + customProps.add(new CustomDocumentProperty(prop.Name, propValue.toString(), prop.Type.getTypeName())); + } + } + } + } + return customProps; + } + + public Map getDocumentProperties() { + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + Map docProps = new HashMap(); + docProps.put(DOC_AUTHOR, documentProperties.getAuthor()); + docProps.put(DOC_TITLE, documentProperties.getTitle()); + docProps.put(DOC_SUBJECT, documentProperties.getSubject()); + docProps.put(DOC_DESCRIPTION, documentProperties.getDescription()); + String[] keywords = documentProperties.getKeywords(); + StringBuilder keys = new StringBuilder(); + for (int i = 0; i < keywords.length; i++) { + if (i > 0) { + keys.append(","); + } + keys.append(keywords[i]); + } + docProps.put(DOC_KEYWORDS, keys.toString()); + + return docProps; + } + + public void setDocumentProperties(Map docProps) { + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + if (docProps.containsKey(DOC_AUTHOR)) { + documentProperties.setAuthor(docProps.get(DOC_AUTHOR)); + System.out.println("set author" + docProps.get(DOC_AUTHOR)); + } + if (docProps.containsKey(DOC_TITLE)) { + documentProperties.setTitle(docProps.get(DOC_TITLE)); + } + if (docProps.containsKey(DOC_SUBJECT)) { + documentProperties.setSubject(docProps.get(DOC_SUBJECT)); + } + if (docProps.containsKey(DOC_DESCRIPTION)) { + documentProperties.setDescription(docProps.get(DOC_DESCRIPTION)); + } + if (docProps.containsKey(DOC_KEYWORDS)) { + documentProperties.setKeywords(docProps.get(DOC_KEYWORDS).split(",")); + } + } + + private void updateDocProperty(String propertyName, Map docProps) { + + } + + private void logDocumentProperties() { + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + System.out.println(" Author: " + documentProperties.getAuthor()); + System.out.println(" Title: " + documentProperties.getTitle()); + System.out.println(" Subject: " + documentProperties.getSubject()); + System.out.println(" Description: " + documentProperties.getDescription()); + } + + private void logDocumentCustomProperties() { + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties(); + XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, userDifinedProperties); + if (propertySet != null) { + XPropertySetInfo propertySetInfo = propertySet.getPropertySetInfo(); + if (propertySetInfo != null) { + Property[] props = propertySetInfo.getProperties(); + Arrays.sort(props, new Comparator() { + public int compare(Property p1, Property p2) { + return (p1.Name).compareTo(p2.Name); + } + }); + + for (Property prop : props) { + Object propValue = getProperty(propertySet, prop.Name); + System.out.println(" " + prop.Name + ": " + prop.Type.getTypeName() + " == " + propValue); + } + } + } + } + + private static Object getProperty(XPropertySet xProps, String propName) { + Object value = null; + try { + value = xProps.getPropertyValue(propName); + } catch (Exception e) { + System.out.println("Could not get property " + propName); + } + return value; + } +} diff --git a/source/pro/litvinovg/libreoffice/metadata/resources/clock128.gif b/source/pro/litvinovg/libreoffice/metadata/resources/clock128.gif new file mode 100644 index 0000000..73d3505 Binary files /dev/null and b/source/pro/litvinovg/libreoffice/metadata/resources/clock128.gif differ diff --git a/source/pro/litvinovg/libreoffice/metadata/resources/cura.png b/source/pro/litvinovg/libreoffice/metadata/resources/cura.png new file mode 100644 index 0000000..c311b86 Binary files /dev/null and b/source/pro/litvinovg/libreoffice/metadata/resources/cura.png differ diff --git a/source/pro/litvinovg/libreoffice/metadata/resources/eclipse.png b/source/pro/litvinovg/libreoffice/metadata/resources/eclipse.png new file mode 100644 index 0000000..3cc80f4 Binary files /dev/null and b/source/pro/litvinovg/libreoffice/metadata/resources/eclipse.png differ diff --git a/source/pro/litvinovg/libreoffice/metadata/resources/webcam.png b/source/pro/litvinovg/libreoffice/metadata/resources/webcam.png new file mode 100644 index 0000000..4fdb98f Binary files /dev/null and b/source/pro/litvinovg/libreoffice/metadata/resources/webcam.png differ diff --git a/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java b/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java new file mode 100644 index 0000000..5e807c4 --- /dev/null +++ b/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java @@ -0,0 +1,273 @@ +package pro.litvinovg.libreoffice.metadata.views; + +import java.awt.BorderLayout; +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import java.awt.GridBagLayout; +import javax.swing.JCheckBox; +import java.awt.GridBagConstraints; +import javax.swing.JButton; +import java.awt.Insets; +import javax.swing.BoxLayout; +import javax.swing.UIManager; +import java.awt.Toolkit; +import javax.swing.JTabbedPane; +import java.awt.GridLayout; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JMenu; +import javax.swing.JTable; +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableModel; + +import pro.litvinovg.libreoffice.metadata.CustomDocumentProperty; +import pro.litvinovg.libreoffice.metadata.Document; + +import javax.swing.JScrollPane; +import javax.swing.JPopupMenu; +import java.awt.Component; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Vector; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +public class EditorGUI extends JFrame { + //Components + private JTable tableDocProps; + private JTable tableDocCustomProps; + + + //Component models + private DefaultTableModel docPropertiesModel; + private DefaultTableModel docCustomPropsModel; + + + private Map docProps; + private ArrayList docCustomProps; + private Document document = null; + private JButton btnNewButton; + + + /** + * Launch the application. + */ + public static void main(String[] args) { + testGUI(); + } + + public static void runGUI(Document doc) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Throwable e) { + e.printStackTrace(); + } + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + EditorGUI frame = new EditorGUI(doc); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + private static void testGUI() { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Throwable e) { + e.printStackTrace(); + } + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + EditorGUI frame = new EditorGUI(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + public EditorGUI(Document doc) { + this.document = doc; + docProps = document.getDocumentProperties(); + docCustomProps = document.getDocumentCustomProperties(); + initComponents(); + createEvents(); + } + + public EditorGUI() { + initComponents(); + createEvents(); + } + + private void createEvents() { + btnNewButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + updateDocProps(); + document.setDocumentProperties(docProps); + } + }); + } + private void updateDocProps() { + System.out.println("updateDocProps"); + Vector dataVector = docPropertiesModel.getDataVector(); + Enumeration elements = dataVector.elements(); + docProps.clear(); + while (elements.hasMoreElements()) { + Vector row = (Vector) elements.nextElement(); + String name = (String) row.get(0); + String value = (String) row.get(1); + docProps.put(name, value); + } + } + private void initComponents() { + setIconImage(Toolkit.getDefaultToolkit().getImage(EditorGUI.class.getResource("/pro/litvinovg/libreoffice/metadata/resources/webcam.png"))); + setTitle("Редактор метаданных"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 1064, 733); + getContentPane().setLayout(null); + getContentPane().setLayout(new GridLayout(0, 1, 0, 0)); + + JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT); + tabbedPane.setBounds(125, 138, 913, 546); + getContentPane().add(tabbedPane); + + JPanel panel = new JPanel(); + tabbedPane.addTab("Документ", null, panel, null); + tabbedPane.setEnabledAt(0, true); + + + createDocPropsModel(); + createCustomDocPropsModel(); + + + tableDocProps = new JTable(); + tableDocProps.setCellSelectionEnabled(true); + tableDocProps.setFillsViewportHeight(true); + tableDocProps.setModel(docPropertiesModel); + + tableDocCustomProps = new JTable(); + tableDocCustomProps.setFillsViewportHeight(true); + tableDocCustomProps.setCellSelectionEnabled(true); + tableDocCustomProps.setModel(docCustomPropsModel); + + btnNewButton = new JButton("Save"); + + GroupLayout gl_panel = new GroupLayout(panel); + gl_panel.setHorizontalGroup( + gl_panel.createParallelGroup(Alignment.TRAILING) + .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup() + .addContainerGap() + .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) + .addGroup(gl_panel.createSequentialGroup() + .addComponent(tableDocProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup() + .addComponent(btnNewButton) + .addGap(58)) + .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup() + .addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE) + .addContainerGap()))) + ); + gl_panel.setVerticalGroup( + gl_panel.createParallelGroup(Alignment.LEADING) + .addGroup(gl_panel.createSequentialGroup() + .addGap(86) + .addComponent(tableDocProps, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE) + .addGap(18) + .addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE) + .addGap(75) + .addComponent(btnNewButton) + .addGap(27)) + ); + panel.setLayout(gl_panel); + + + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + + JMenu mnFile = new JMenu("Файл"); + menuBar.add(mnFile); + + JMenu mnEdit = new JMenu("Edit"); + menuBar.add(mnEdit); + } + + private void createDocPropsModel() { + Object[][] tableArray = new Object[][] { + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + }; + if (docProps != null) { + Object[] arrayOfEntries = docProps.entrySet().toArray(); + tableArray = new Object[docProps.size()][2]; + for (int i = 0; i < tableArray.length; i++) { + Entry entry = (Entry) arrayOfEntries[i]; + tableArray[i][0] = entry.getKey(); + tableArray[i][1] = entry.getValue(); + } + } + docPropertiesModel = new DefaultTableModel( + tableArray, + new String[] { + "Metadata name", "Metadata value" + }); + } + + private void createCustomDocPropsModel() { + Object[][] tableArray = new Object[][] { + {null, null}, + {null, null}, + {null, null}, + {null, null}, + }; + if (docCustomProps != null) { + tableArray = new Object[docCustomProps.size()][2]; + for (int i = 0; i < tableArray.length; i++) { + CustomDocumentProperty curProp = docCustomProps.get(i); + tableArray[i][0] = curProp.getName(); + tableArray[i][1] = curProp.getValue(); + } + } + docCustomPropsModel = new DefaultTableModel( + tableArray, + new String[] { + "Metadata name", "Metadata value" + }); + } + + + private static void addPopup(Component component, final JPopupMenu popup) { + component.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } + } + public void mouseReleased(MouseEvent e) { + if (e.isPopupTrigger()) { + showMenu(e); + } + } + private void showMenu(MouseEvent e) { + popup.show(e.getComponent(), e.getX(), e.getY()); + } + }); + } +}