Works for document properties.
This commit is contained in:
parent
f9a9aefc72
commit
425bd82592
3 changed files with 126 additions and 22 deletions
|
@ -0,0 +1,7 @@
|
||||||
|
package org.libreoffice.example.comp;
|
||||||
|
|
||||||
|
public class MetadataInaccessableException extends Exception {
|
||||||
|
public MetadataInaccessableException(String errorMessage) {
|
||||||
|
super(errorMessage);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package pro.litvinovg.libreoffice.metadata;
|
package pro.litvinovg.libreoffice.metadata;
|
||||||
|
|
||||||
|
import java.nio.file.attribute.UserDefinedFileAttributeView;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
@ -8,13 +9,18 @@ import java.util.Map;
|
||||||
import org.libreoffice.example.comp.MetadataInaccessableException;
|
import org.libreoffice.example.comp.MetadataInaccessableException;
|
||||||
|
|
||||||
import com.sun.org.apache.bcel.internal.generic.NEW;
|
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.Property;
|
||||||
|
import com.sun.star.beans.PropertyExistException;
|
||||||
|
import com.sun.star.beans.UnknownPropertyException;
|
||||||
import com.sun.star.beans.XPropertyContainer;
|
import com.sun.star.beans.XPropertyContainer;
|
||||||
import com.sun.star.beans.XPropertySet;
|
import com.sun.star.beans.XPropertySet;
|
||||||
import com.sun.star.beans.XPropertySetInfo;
|
import com.sun.star.beans.XPropertySetInfo;
|
||||||
import com.sun.star.document.XDocumentProperties;
|
import com.sun.star.document.XDocumentProperties;
|
||||||
import com.sun.star.document.XDocumentPropertiesSupplier;
|
import com.sun.star.document.XDocumentPropertiesSupplier;
|
||||||
import com.sun.star.frame.XDesktop;
|
import com.sun.star.frame.XDesktop;
|
||||||
|
import com.sun.star.lang.IllegalArgumentException;
|
||||||
import com.sun.star.lang.XComponent;
|
import com.sun.star.lang.XComponent;
|
||||||
import com.sun.star.lang.XMultiComponentFactory;
|
import com.sun.star.lang.XMultiComponentFactory;
|
||||||
import com.sun.star.uno.UnoRuntime;
|
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_SUBJECT = "Document subject";
|
||||||
private static final String DOC_DESCRIPTION = "Document description";
|
private static final String DOC_DESCRIPTION = "Document description";
|
||||||
private static final String DOC_KEYWORDS = "Document keywords";
|
private static final String DOC_KEYWORDS = "Document keywords";
|
||||||
|
private static final short REMOVEABLE_ATTRIBUTE = 128;
|
||||||
|
|
||||||
private XComponentContext context;
|
private XComponentContext context;
|
||||||
private XDesktop xDesktop;
|
private XDesktop xDesktop;
|
||||||
|
@ -48,7 +54,7 @@ public class Document {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
logProperties();
|
//logProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getCurrentDocument() throws MetadataInaccessableException {
|
private void getCurrentDocument() throws MetadataInaccessableException {
|
||||||
|
@ -63,6 +69,60 @@ public class Document {
|
||||||
logDocumentCustomProperties();
|
logDocumentCustomProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCustomDocumentProperties(ArrayList<CustomDocumentProperty> docCustomProps) {
|
||||||
|
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
|
||||||
|
documentProperties = documentPropertiesSupplier.getDocumentProperties();
|
||||||
|
removeStringProperties();
|
||||||
|
addStringProperties(docCustomProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addStringProperties(ArrayList<CustomDocumentProperty> 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<CustomDocumentProperty> getDocumentCustomProperties() {
|
public ArrayList<CustomDocumentProperty> getDocumentCustomProperties() {
|
||||||
ArrayList<CustomDocumentProperty> customProps = new ArrayList<CustomDocumentProperty>();
|
ArrayList<CustomDocumentProperty> customProps = new ArrayList<CustomDocumentProperty>();
|
||||||
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
|
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
|
||||||
|
@ -116,7 +176,6 @@ public class Document {
|
||||||
documentProperties = documentPropertiesSupplier.getDocumentProperties();
|
documentProperties = documentPropertiesSupplier.getDocumentProperties();
|
||||||
if (docProps.containsKey(DOC_AUTHOR)) {
|
if (docProps.containsKey(DOC_AUTHOR)) {
|
||||||
documentProperties.setAuthor(docProps.get(DOC_AUTHOR));
|
documentProperties.setAuthor(docProps.get(DOC_AUTHOR));
|
||||||
System.out.println("set author" + docProps.get(DOC_AUTHOR));
|
|
||||||
}
|
}
|
||||||
if (docProps.containsKey(DOC_TITLE)) {
|
if (docProps.containsKey(DOC_TITLE)) {
|
||||||
documentProperties.setTitle(docProps.get(DOC_TITLE));
|
documentProperties.setTitle(docProps.get(DOC_TITLE));
|
||||||
|
@ -132,10 +191,6 @@ public class Document {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateDocProperty(String propertyName, Map<String, String> docProps) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void logDocumentProperties() {
|
private void logDocumentProperties() {
|
||||||
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
|
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
|
||||||
documentProperties = documentPropertiesSupplier.getDocumentProperties();
|
documentProperties = documentPropertiesSupplier.getDocumentProperties();
|
||||||
|
@ -177,4 +232,6 @@ public class Document {
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,10 @@ import java.util.Map.Entry;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.ActionEvent;
|
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 {
|
public class EditorGUI extends JFrame {
|
||||||
//Components
|
//Components
|
||||||
|
@ -56,6 +60,7 @@ public class EditorGUI extends JFrame {
|
||||||
private ArrayList<CustomDocumentProperty> docCustomProps;
|
private ArrayList<CustomDocumentProperty> docCustomProps;
|
||||||
private Document document = null;
|
private Document document = null;
|
||||||
private JButton btnNewButton;
|
private JButton btnNewButton;
|
||||||
|
private JMenuItem menuItemPupupDocUserProps;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,6 +80,7 @@ public class EditorGUI extends JFrame {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
EditorGUI frame = new EditorGUI(doc);
|
EditorGUI frame = new EditorGUI(doc);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -93,6 +99,7 @@ public class EditorGUI extends JFrame {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
EditorGUI frame = new EditorGUI();
|
EditorGUI frame = new EditorGUI();
|
||||||
|
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -118,11 +125,32 @@ public class EditorGUI extends JFrame {
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
updateDocProps();
|
updateDocProps();
|
||||||
document.setDocumentProperties(docProps);
|
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() {
|
private void updateDocProps() {
|
||||||
System.out.println("updateDocProps");
|
|
||||||
Vector dataVector = docPropertiesModel.getDataVector();
|
Vector dataVector = docPropertiesModel.getDataVector();
|
||||||
Enumeration elements = dataVector.elements();
|
Enumeration elements = dataVector.elements();
|
||||||
docProps.clear();
|
docProps.clear();
|
||||||
|
@ -155,11 +183,16 @@ public class EditorGUI extends JFrame {
|
||||||
|
|
||||||
|
|
||||||
tableDocProps = new JTable();
|
tableDocProps = new JTable();
|
||||||
|
tableDocProps.putClientProperty("terminateEditOnFocusLost", true);
|
||||||
|
tableDocProps.setSurrendersFocusOnKeystroke(true);
|
||||||
tableDocProps.setCellSelectionEnabled(true);
|
tableDocProps.setCellSelectionEnabled(true);
|
||||||
tableDocProps.setFillsViewportHeight(true);
|
tableDocProps.setFillsViewportHeight(true);
|
||||||
tableDocProps.setModel(docPropertiesModel);
|
tableDocProps.setModel(docPropertiesModel);
|
||||||
|
|
||||||
tableDocCustomProps = new JTable();
|
tableDocCustomProps = new JTable();
|
||||||
|
tableDocCustomProps.putClientProperty("terminateEditOnFocusLost", true);
|
||||||
|
|
||||||
|
tableDocCustomProps.setSurrendersFocusOnKeystroke(true);
|
||||||
tableDocCustomProps.setFillsViewportHeight(true);
|
tableDocCustomProps.setFillsViewportHeight(true);
|
||||||
tableDocCustomProps.setCellSelectionEnabled(true);
|
tableDocCustomProps.setCellSelectionEnabled(true);
|
||||||
tableDocCustomProps.setModel(docCustomPropsModel);
|
tableDocCustomProps.setModel(docCustomPropsModel);
|
||||||
|
@ -168,31 +201,38 @@ public class EditorGUI extends JFrame {
|
||||||
|
|
||||||
GroupLayout gl_panel = new GroupLayout(panel);
|
GroupLayout gl_panel = new GroupLayout(panel);
|
||||||
gl_panel.setHorizontalGroup(
|
gl_panel.setHorizontalGroup(
|
||||||
gl_panel.createParallelGroup(Alignment.TRAILING)
|
gl_panel.createParallelGroup(Alignment.LEADING)
|
||||||
.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
|
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
|
||||||
|
.addContainerGap(838, Short.MAX_VALUE)
|
||||||
|
.addComponent(btnNewButton)
|
||||||
|
.addGap(58))
|
||||||
|
.addGroup(gl_panel.createSequentialGroup()
|
||||||
.addContainerGap()
|
.addContainerGap()
|
||||||
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
|
.addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
|
||||||
.addGroup(gl_panel.createSequentialGroup()
|
.addContainerGap())
|
||||||
.addComponent(tableDocProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
|
.addGroup(gl_panel.createSequentialGroup()
|
||||||
.addContainerGap())
|
.addContainerGap()
|
||||||
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
|
.addComponent(tableDocProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
|
||||||
.addComponent(btnNewButton)
|
.addContainerGap())
|
||||||
.addGap(58))
|
|
||||||
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
|
|
||||||
.addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
|
|
||||||
.addContainerGap())))
|
|
||||||
);
|
);
|
||||||
gl_panel.setVerticalGroup(
|
gl_panel.setVerticalGroup(
|
||||||
gl_panel.createParallelGroup(Alignment.LEADING)
|
gl_panel.createParallelGroup(Alignment.LEADING)
|
||||||
.addGroup(gl_panel.createSequentialGroup()
|
.addGroup(gl_panel.createSequentialGroup()
|
||||||
.addGap(86)
|
.addGap(86)
|
||||||
.addComponent(tableDocProps, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
|
.addComponent(tableDocProps, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
|
||||||
.addGap(18)
|
.addPreferredGap(ComponentPlacement.UNRELATED)
|
||||||
.addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
|
.addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
|
||||||
.addGap(75)
|
.addGap(81)
|
||||||
.addComponent(btnNewButton)
|
.addComponent(btnNewButton)
|
||||||
.addGap(27))
|
.addGap(27))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
JPopupMenu popupMenu = new JPopupMenu();
|
||||||
|
addPopup(tableDocCustomProps, popupMenu);
|
||||||
|
|
||||||
|
menuItemPupupDocUserProps = new JMenuItem("Добавить строку");
|
||||||
|
|
||||||
|
popupMenu.add(menuItemPupupDocUserProps);
|
||||||
panel.setLayout(gl_panel);
|
panel.setLayout(gl_panel);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue