diff --git a/source/org/libreoffice/example/comp/MetadataInaccessableException.java b/source/org/libreoffice/example/comp/MetadataInaccessableException.java new file mode 100644 index 0000000..da1ccb2 --- /dev/null +++ b/source/org/libreoffice/example/comp/MetadataInaccessableException.java @@ -0,0 +1,7 @@ +package org.libreoffice.example.comp; + +public class MetadataInaccessableException extends Exception { + public MetadataInaccessableException(String errorMessage) { + super(errorMessage); + } +} diff --git a/source/pro/litvinovg/libreoffice/metadata/Document.java b/source/pro/litvinovg/libreoffice/metadata/Document.java index ab9cc25..178c9ce 100644 --- a/source/pro/litvinovg/libreoffice/metadata/Document.java +++ b/source/pro/litvinovg/libreoffice/metadata/Document.java @@ -1,5 +1,6 @@ package pro.litvinovg.libreoffice.metadata; +import java.nio.file.attribute.UserDefinedFileAttributeView; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -8,13 +9,18 @@ import java.util.Map; import org.libreoffice.example.comp.MetadataInaccessableException; import com.sun.org.apache.bcel.internal.generic.NEW; +import com.sun.star.beans.IllegalTypeException; +import com.sun.star.beans.NotRemoveableException; import com.sun.star.beans.Property; +import com.sun.star.beans.PropertyExistException; +import com.sun.star.beans.UnknownPropertyException; 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.IllegalArgumentException; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.UnoRuntime; @@ -27,7 +33,7 @@ public class Document { 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 static final short REMOVEABLE_ATTRIBUTE = 128; private XComponentContext context; private XDesktop xDesktop; @@ -48,7 +54,7 @@ public class Document { e.printStackTrace(); System.exit(1); } - logProperties(); + //logProperties(); } private void getCurrentDocument() throws MetadataInaccessableException { @@ -62,7 +68,61 @@ public class Document { logDocumentProperties(); logDocumentCustomProperties(); } + + public void setCustomDocumentProperties(ArrayList docCustomProps) { + documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); + documentProperties = documentPropertiesSupplier.getDocumentProperties(); + removeStringProperties(); + addStringProperties(docCustomProps); + } + private void addStringProperties(ArrayList docCustomProps) { + XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties(); + for (int i = 0; i < docCustomProps.size();i++) { + CustomDocumentProperty property = docCustomProps.get(i); + try { + userDifinedProperties.addProperty(property.getName(), REMOVEABLE_ATTRIBUTE, property.getValue()); + // System.out.println("added "+ property.getName() + " value " + property.getValue()); + + } catch (IllegalArgumentException e) { + System.out.println("IllegalArgumentException while adding new property"); + e.printStackTrace(); + } catch (PropertyExistException e) { + System.out.println("Property already exists"); + e.printStackTrace(); + } catch (IllegalTypeException e) { + System.out.println("Property type illegal"); + e.printStackTrace(); + } + } + + } + + private void removeStringProperties() { + 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(); + for (Property prop : props) { + if (prop.Type.getTypeName().equals("string")){ + try { + userDifinedProperties.removeProperty(prop.Name); + // System.out.println("removed "+ prop.Name); + } catch (UnknownPropertyException e) { + System.out.println("Property " + prop.Name + " does not exist."); + e.printStackTrace(); + } catch (NotRemoveableException e) { + System.out.println("Property " + prop.Name + " is not removeable."); + e.printStackTrace(); + } + } + } + } + } + } + public ArrayList getDocumentCustomProperties() { ArrayList customProps = new ArrayList(); documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); @@ -116,7 +176,6 @@ public class Document { 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)); @@ -132,10 +191,6 @@ public class Document { } } - private void updateDocProperty(String propertyName, Map docProps) { - - } - private void logDocumentProperties() { documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument); documentProperties = documentPropertiesSupplier.getDocumentProperties(); @@ -177,4 +232,6 @@ public class Document { } return value; } + + } diff --git a/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java b/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java index 5e807c4..9ab7fd6 100644 --- a/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java +++ b/source/pro/litvinovg/libreoffice/metadata/views/EditorGUI.java @@ -40,6 +40,10 @@ import java.util.Map.Entry; import java.util.Vector; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; +import javax.swing.JToggleButton; +import javax.swing.LayoutStyle.ComponentPlacement; public class EditorGUI extends JFrame { //Components @@ -56,6 +60,7 @@ public class EditorGUI extends JFrame { private ArrayList docCustomProps; private Document document = null; private JButton btnNewButton; + private JMenuItem menuItemPupupDocUserProps; /** @@ -75,6 +80,7 @@ public class EditorGUI extends JFrame { public void run() { try { EditorGUI frame = new EditorGUI(doc); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); @@ -93,6 +99,7 @@ public class EditorGUI extends JFrame { public void run() { try { EditorGUI frame = new EditorGUI(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); @@ -118,11 +125,32 @@ public class EditorGUI extends JFrame { public void actionPerformed(ActionEvent e) { updateDocProps(); document.setDocumentProperties(docProps); + updateDocCustomProps(); + document.setCustomDocumentProperties(docCustomProps); + } + }); + + menuItemPupupDocUserProps.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + docCustomPropsModel.addRow(new Object[] {null, null}); } }); } + private void updateDocCustomProps() { + Vector dataVector = docCustomPropsModel.getDataVector(); + Enumeration elements = dataVector.elements(); + docCustomProps.clear(); + while (elements.hasMoreElements()) { + Vector row = (Vector) elements.nextElement(); + String name = (String) row.get(0); + String value = (String) row.get(1); + if (!name.isEmpty() && !value.isEmpty()) { + docCustomProps.add(new CustomDocumentProperty(name, value, "string")); + } + } + } + private void updateDocProps() { - System.out.println("updateDocProps"); Vector dataVector = docPropertiesModel.getDataVector(); Enumeration elements = dataVector.elements(); docProps.clear(); @@ -155,11 +183,16 @@ public class EditorGUI extends JFrame { tableDocProps = new JTable(); + tableDocProps.putClientProperty("terminateEditOnFocusLost", true); + tableDocProps.setSurrendersFocusOnKeystroke(true); tableDocProps.setCellSelectionEnabled(true); tableDocProps.setFillsViewportHeight(true); tableDocProps.setModel(docPropertiesModel); tableDocCustomProps = new JTable(); + tableDocCustomProps.putClientProperty("terminateEditOnFocusLost", true); + + tableDocCustomProps.setSurrendersFocusOnKeystroke(true); tableDocCustomProps.setFillsViewportHeight(true); tableDocCustomProps.setCellSelectionEnabled(true); tableDocCustomProps.setModel(docCustomPropsModel); @@ -168,31 +201,38 @@ public class EditorGUI extends JFrame { GroupLayout gl_panel = new GroupLayout(panel); gl_panel.setHorizontalGroup( - gl_panel.createParallelGroup(Alignment.TRAILING) - .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup() + gl_panel.createParallelGroup(Alignment.LEADING) + .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup() + .addContainerGap(838, Short.MAX_VALUE) + .addComponent(btnNewButton) + .addGap(58)) + .addGroup(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()))) + .addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(gl_panel.createSequentialGroup() + .addContainerGap() + .addComponent(tableDocProps, 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) + .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE) - .addGap(75) + .addGap(81) .addComponent(btnNewButton) .addGap(27)) ); + + JPopupMenu popupMenu = new JPopupMenu(); + addPopup(tableDocCustomProps, popupMenu); + + menuItemPupupDocUserProps = new JMenuItem("Добавить строку"); + + popupMenu.add(menuItemPupupDocUserProps); panel.setLayout(gl_panel);