Working on GUI

This commit is contained in:
Georgy Litvinov 2020-04-16 14:08:29 +02:00
parent 76859c54db
commit adf19bc4ab
4 changed files with 190 additions and 140 deletions

View file

@ -15,6 +15,7 @@ import static pro.litvinovg.libreoffice.metadata.Document.*;
public class MetadataReader extends DocumentParserImpl { public class MetadataReader extends DocumentParserImpl {
private ArrayList<OutlineElement> outline; private ArrayList<OutlineElement> outline;
private boolean firstPara = true;
public MetadataReader(XText text, ArrayList<OutlineElement> outline) { public MetadataReader(XText text, ArrayList<OutlineElement> outline) {
super(text); super(text);
@ -24,7 +25,6 @@ public class MetadataReader extends DocumentParserImpl {
public void parse() { public void parse() {
parseText(text); parseText(text);
OutlineElement lastElement = outline.get(outline.size()-1); OutlineElement lastElement = outline.get(outline.size()-1);
lastElement.setAnnotationEnd(documentEnd);
} }
@Override @Override
@ -59,11 +59,14 @@ public class MetadataReader extends DocumentParserImpl {
} catch (WrappedTargetException e) { } catch (WrappedTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (outlineLevel != null && outlineLevel > 0) { if (firstPara || (outlineLevel != null && outlineLevel > 0)) {
XTextRange textRange = textContent.getAnchor(); XTextRange textRange = textContent.getAnchor();
OutlineElement lastElement = outline.get(outline.size()-1); if (firstPara) {
lastElement.setAnnotationEnd(textContent.getAnchor().getStart()); outline.add(new OutlineElement(textRange, "Документ"));
outline.add(new OutlineElement(textRange)); firstPara = false;
} else {
outline.add(new OutlineElement(textRange, textRange.getString()));
}
} }
} }
} }

View file

@ -82,12 +82,12 @@ public class Document {
reader.parse(); reader.parse();
} }
private void writeMetadata() { private void writeOutlineMetadata() {
cleanMetadataInDocument(); cleanMetadataInDocument();
for (OutlineElement element : outline){ for (OutlineElement element : outline){
Object annotation; Object annotation;
try { try {
annotation = multiServiceFactory.createInstance("com.sun.star.text.textfield.nnotation"); annotation = multiServiceFactory.createInstance("com.sun.star.text.textfield.annotation");
element.writeMetadata(annotation); element.writeMetadata(annotation);
} catch (com.sun.star.uno.Exception e) { } catch (com.sun.star.uno.Exception e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -19,25 +19,38 @@ import com.sun.star.uno.UnoRuntime;
import static pro.litvinovg.libreoffice.metadata.Document.*; import static pro.litvinovg.libreoffice.metadata.Document.*;
public class OutlineElement { public class OutlineElement {
private String name; private String elementName;
private ArrayList<MetadataElement> metadata = null; private ArrayList<MetadataElement> metadata = null;
public String getName() { public String getName() {
return name; return elementName;
} }
private XTextRange textRange; private XTextRange textRange;
private XTextRange annotationStart = null;
private XTextRange annotationEnd = null;
public OutlineElement(XTextRange textRange) { public OutlineElement(XTextRange textRange, String name) {
this.textRange = textRange; this.textRange = textRange;
this.name = textRange.getString(); this.elementName = name;
this.metadata = new ArrayList<MetadataElement>(); this.metadata = new ArrayList<MetadataElement>();
annotationStart = textRange.getStart();
} }
public void setAnnotationEnd(XTextRange anchor) {
this.annotationEnd = anchor; public Object[][] metadataToArray() {
Object [] [] result = null;
int size = metadata.size();
if (metadata.size() == 0) {
result = new Object[][] {
{null, null},
{null, null},
{null, null},
};
} else {
result = new Object [size] [2];
for (int i = 0; i < size; i++) {
result[i][0] = metadata.get(i).getName();
result[i][1] = metadata.get(i).getValue();
}
}
return result;
} }
public void readMetadata(String data) { public void readMetadata(String data) {
@ -59,24 +72,23 @@ public class OutlineElement {
private String metadataToString() { private String metadataToString() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
for (MetadataElement element : metadata) { for (MetadataElement element : metadata) {
String name = element.getName();
String value = element.getValue();
if (!name.isEmpty() && !value.isEmpty()) {
json.put(element.getName(), element.getValue()); json.put(element.getName(), element.getValue());
}
} }
return json.toString(); return json.toString();
} }
public void writeMetadata(Object annotation) { public void writeMetadata(Object annotation) {
XPropertySet properties = UnoRuntime.queryInterface(XPropertySet.class, annotation); XPropertySet properties = UnoRuntime.queryInterface(XPropertySet.class, annotation);
XText startText = annotationStart.getText(); XText startText = textRange.getText();
try { try {
properties.setPropertyValue("Author", METADATA_EXTENSION); properties.setPropertyValue("Author", METADATA_EXTENSION);
properties.setPropertyValue("Content", metadataToString()); properties.setPropertyValue("Content", metadataToString());
XTextRange annotationRange = null; XTextRange annotationRange = null;
if (startText.equals(annotationEnd.getText())){
XTextCursor cursor = startText.createTextCursorByRange(annotationStart);
cursor.gotoRange(annotationEnd, true);
annotationRange = UnoRuntime.queryInterface(XTextRange.class, cursor);
} else {
annotationRange = textRange; annotationRange = textRange;
}
XTextField textField = UnoRuntime.queryInterface(XTextField.class, annotation); XTextField textField = UnoRuntime.queryInterface(XTextField.class, annotation);
startText.insertTextContent( annotationRange, textField, true ); startText.insertTextContent( annotationRange, textField, true );
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {

View file

@ -49,24 +49,27 @@ import javax.swing.JLabel;
public class EditorGUI extends JFrame { public class EditorGUI extends JFrame {
//Components //Components
private JTable tableDocProps; private JTable tableDocStandard;
private JTable tableDocCustomProps; private JTable tableDocCustom;
//Component models //Component models
private DefaultTableModel docPropertiesModel; private DefaultTableModel docPropertiesModel;
private DefaultTableModel docCustomPropsModel; private DefaultTableModel docCustomPropsModel;
private DefaultTableModel outlineTableModel;
//GroupLayouts
private Map<String, String> docProps; private Map<String, String> docProps;
private ArrayList<CustomDocumentProperty> docCustomProps; private ArrayList<CustomDocumentProperty> docCustomProps;
private Document document = null; private Document document = null;
private JButton btnNewButton; private JButton btnSaveButton;
private JMenuItem menuItemPupupDocUserProps; private JMenuItem menuItemPupupDocUserProps;
private ArrayList<OutlineElement> outline; private ArrayList<OutlineElement> outline;
private JTable tableOutline; private JTabbedPane tabbedPane;
private JPanel panelForOutline; private JPanel panelStandardMetadata;
private JMenuBar menuBar;
/** /**
@ -129,12 +132,10 @@ public class EditorGUI extends JFrame {
} }
private void createEvents() { private void createEvents() {
btnNewButton.addActionListener(new ActionListener() {
btnSaveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
updateDocProps(); saveAction();
document.setDocumentProperties(docProps);
updateDocCustomProps();
document.setCustomDocumentProperties(docCustomProps);
} }
}); });
@ -170,135 +171,163 @@ public class EditorGUI extends JFrame {
} }
} }
private void initComponents() { private void initComponents() {
setIconImage(Toolkit.getDefaultToolkit().getImage(EditorGUI.class.getResource("/pro/litvinovg/libreoffice/metadata/resources/webcam.png"))); configureWindow();
setTitle("Редактор метаданных"); panelStandardMetadata = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tabbedPane.addTab("Метаданные либры", null, panelStandardMetadata, null);
setBounds(100, 100, 1064, 733); tabbedPane.setEnabledAt(0, true);
getContentPane().setLayout(null); if (true) {
getContentPane().setLayout(new GridLayout(0, 1, 0, 0)); //TEST ONLY
addMetadataTab(new OutlineElement(null, "Тестовое название"));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT); }
tabbedPane.setBounds(125, 138, 913, 546); for (OutlineElement element : outline) {
getContentPane().add(tabbedPane); addMetadataTab(element);
JPanel panel = new JPanel();
tabbedPane.addTab("Документ", null, panel, null);
for (OutlineElement element : outline) {
JPanel panelForOutline = new JPanel();
tabbedPane.addTab(element.getName(), null, panelForOutline, null);
} }
panelForOutline = new JPanel();
tabbedPane.addTab("секция", null, panelForOutline, null);
tableOutline = new JTable(); createDocPropsModel();
createOutlineTableModel(); createCustomDocPropsModel();
tableDocStandard = new JTable();
tableDocCustom = new JTable();
configureTableDocStandard();
configureTableDocCustom();
btnSaveButton = new JButton("Сохранить");
GroupLayout gl_panelStandardMetadata = new GroupLayout(panelStandardMetadata);
configureGroupLayoutDoc(gl_panelStandardMetadata);
JPopupMenu popupMenu = new JPopupMenu();
addPopup(tableDocCustom, popupMenu);
menuItemPupupDocUserProps = new JMenuItem("Добавить строку");
popupMenu.add(menuItemPupupDocUserProps);
panelStandardMetadata.setLayout(gl_panelStandardMetadata);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("Файл");
menuBar.add(mnFile);
}
private void addMetadataTab(OutlineElement element) {
JPanel panelOutline = new JPanel();
JTable tableOutline = new JTable();
DefaultTableModel outlineTableModel = createOutlineTableModel(element);
configureTableOutline(tableOutline, outlineTableModel);
JButton btnOutlineSave = new JButton("Сохранить");
btnOutlineSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveAction();
}
});
GroupLayout groupLayoutOutlineMetadata = new GroupLayout(panelOutline);
groupLayoutOutlineMetadata.setHorizontalGroup(
groupLayoutOutlineMetadata.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayoutOutlineMetadata.createSequentialGroup()
.addGap(34)
.addComponent(tableOutline, GroupLayout.DEFAULT_SIZE, 839, Short.MAX_VALUE)
.addGap(126))
.addGroup(Alignment.TRAILING, groupLayoutOutlineMetadata.createSequentialGroup()
.addContainerGap(748, Short.MAX_VALUE)
.addComponent(btnOutlineSave)
.addGap(134))
);
groupLayoutOutlineMetadata.setVerticalGroup(
groupLayoutOutlineMetadata.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayoutOutlineMetadata.createSequentialGroup()
.addGap(85)
.addComponent(tableOutline, GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
.addGap(52)
.addComponent(btnOutlineSave)
.addGap(43))
);
panelOutline.setLayout(groupLayoutOutlineMetadata);
tabbedPane.addTab(element.getName(), null, panelOutline, null);
}
private void configureTableOutline(JTable tableOutline, DefaultTableModel outlineTableModel) {
tableOutline.setModel(outlineTableModel); tableOutline.setModel(outlineTableModel);
tableOutline.putClientProperty("terminateEditOnFocusLost", true); tableOutline.putClientProperty("terminateEditOnFocusLost", true);
tableOutline.setSurrendersFocusOnKeystroke(true); tableOutline.setSurrendersFocusOnKeystroke(true);
tableOutline.setCellSelectionEnabled(true); tableOutline.setCellSelectionEnabled(true);
tableOutline.setFillsViewportHeight(true); tableOutline.setFillsViewportHeight(true);
}
GroupLayout gl_panelForOutline = new GroupLayout(panelForOutline); private void configureWindow() {
gl_panelForOutline.setHorizontalGroup( setIconImage(Toolkit.getDefaultToolkit().getImage(EditorGUI.class.getResource("/pro/litvinovg/libreoffice/metadata/resources/webcam.png")));
gl_panelForOutline.createParallelGroup(Alignment.LEADING) setTitle("Редактор метаданных");
.addGroup(gl_panelForOutline.createSequentialGroup() setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
.addGap(34) setBounds(100, 100, 1170, 755);
.addComponent(tableOutline, GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE) getContentPane().setLayout(null);
.addGap(126)) getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
);
gl_panelForOutline.setVerticalGroup(
gl_panelForOutline.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panelForOutline.createSequentialGroup()
.addGap(85)
.addComponent(tableOutline, GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
.addGap(120))
);
panelForOutline.setLayout(gl_panelForOutline);
tabbedPane.setEnabledAt(0, true); tabbedPane = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setBounds(125, 138, 913, 546);
getContentPane().add(tabbedPane);
}
private void configureTableDocStandard() {
tableDocStandard.putClientProperty("terminateEditOnFocusLost", true);
tableDocStandard.setSurrendersFocusOnKeystroke(true);
tableDocStandard.setCellSelectionEnabled(true);
tableDocStandard.setFillsViewportHeight(true);
tableDocStandard.setModel(docPropertiesModel);
}
createDocPropsModel(); private void configureTableDocCustom() {
createCustomDocPropsModel(); tableDocCustom.setColumnSelectionAllowed(true);
tableDocCustom.putClientProperty("terminateEditOnFocusLost", true);
tableDocCustom.setSurrendersFocusOnKeystroke(true);
tableDocProps = new JTable(); tableDocCustom.setFillsViewportHeight(true);
tableDocProps.putClientProperty("terminateEditOnFocusLost", true); tableDocCustom.setCellSelectionEnabled(true);
tableDocProps.setSurrendersFocusOnKeystroke(true); tableDocCustom.setModel(docCustomPropsModel);
tableDocProps.setCellSelectionEnabled(true); }
tableDocProps.setFillsViewportHeight(true);
tableDocProps.setModel(docPropertiesModel);
tableDocCustomProps = new JTable();
tableDocCustomProps.setColumnSelectionAllowed(true);
tableDocCustomProps.putClientProperty("terminateEditOnFocusLost", true);
tableDocCustomProps.setSurrendersFocusOnKeystroke(true);
tableDocCustomProps.setFillsViewportHeight(true);
tableDocCustomProps.setCellSelectionEnabled(true);
tableDocCustomProps.setModel(docCustomPropsModel);
btnNewButton = new JButton("Save");
private void configureGroupLayoutDoc(GroupLayout gl_panelStandardMetadata) {
JLabel labelDocProperties = new JLabel("Основные метаданные"); JLabel labelDocProperties = new JLabel("Основные метаданные");
JLabel labelDocCustomProperties = new JLabel("Дополнительные метаданные"); JLabel labelDocCustomProperties = new JLabel("Дополнительные метаданные");
gl_panelStandardMetadata.setHorizontalGroup(
GroupLayout gl_panel = new GroupLayout(panel); gl_panelStandardMetadata.createParallelGroup(Alignment.TRAILING)
gl_panel.setHorizontalGroup( .addGroup(Alignment.LEADING, gl_panelStandardMetadata.createSequentialGroup()
gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
.addGap(378) .addGap(378)
.addComponent(labelDocProperties, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE) .addComponent(labelDocProperties, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addGap(421)) .addGap(421))
.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup() .addGroup(Alignment.LEADING, gl_panelStandardMetadata.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panelStandardMetadata.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup() .addGroup(gl_panelStandardMetadata.createSequentialGroup()
.addComponent(tableDocProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE) .addComponent(tableDocStandard, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
.addContainerGap()) .addContainerGap())
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup() .addGroup(Alignment.TRAILING, gl_panelStandardMetadata.createSequentialGroup()
.addComponent(btnNewButton) .addComponent(btnSaveButton)
.addGap(294)))) .addGap(154))))
.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup() .addGroup(Alignment.LEADING, gl_panelStandardMetadata.createSequentialGroup()
.addGap(350) .addGap(350)
.addComponent(labelDocCustomProperties, GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE) .addComponent(labelDocCustomProperties, GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
.addGap(381)) .addGap(381))
.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup() .addGroup(Alignment.LEADING, gl_panelStandardMetadata.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addComponent(tableDocCustomProps, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE) .addComponent(tableDocCustom, GroupLayout.DEFAULT_SIZE, 940, Short.MAX_VALUE)
.addContainerGap()) .addContainerGap())
); );
gl_panel.setVerticalGroup( gl_panelStandardMetadata.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING) gl_panelStandardMetadata.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup() .addGroup(gl_panelStandardMetadata.createSequentialGroup()
.addGap(53) .addGap(53)
.addComponent(labelDocProperties) .addComponent(labelDocProperties)
.addGap(18) .addGap(18)
.addComponent(tableDocProps, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE) .addComponent(tableDocStandard, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED) .addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(labelDocCustomProperties, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE) .addComponent(labelDocCustomProperties, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
.addGap(18) .addGap(18)
.addComponent(tableDocCustomProps, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE) .addComponent(tableDocCustom, GroupLayout.PREFERRED_SIZE, 322, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 90, Short.MAX_VALUE) .addPreferredGap(ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
.addComponent(btnNewButton) .addComponent(btnSaveButton)
.addGap(31)) .addGap(31))
); );
JPopupMenu popupMenu = new JPopupMenu();
addPopup(tableDocCustomProps, popupMenu);
menuItemPupupDocUserProps = new JMenuItem("Добавить строку");
popupMenu.add(menuItemPupupDocUserProps);
panel.setLayout(gl_panel);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("Файл");
menuBar.add(mnFile);
} }
private void createDocPropsModel() { private void createDocPropsModel() {
@ -325,15 +354,15 @@ public class EditorGUI extends JFrame {
}); });
} }
private void createOutlineTableModel() { private DefaultTableModel createOutlineTableModel(OutlineElement element) {
outlineTableModel = new DefaultTableModel(
new Object[][] { DefaultTableModel outlineTableModel = new DefaultTableModel(
{null, null}, element.metadataToArray(),
},
new String[] { new String[] {
"New column", "New column" "Metadata Name", "Metadata Value"
} }
); );
return outlineTableModel;
} }
private void createCustomDocPropsModel() { private void createCustomDocPropsModel() {
Object[][] tableArray = new Object[][] { Object[][] tableArray = new Object[][] {
@ -375,4 +404,10 @@ public class EditorGUI extends JFrame {
} }
}); });
} }
public void saveAction() {
updateDocProps();
document.setDocumentProperties(docProps);
updateDocCustomProps();
document.setCustomDocumentProperties(docCustomProps);
}
} }