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 {
private ArrayList<OutlineElement> outline;
private boolean firstPara = true;
public MetadataReader(XText text, ArrayList<OutlineElement> outline) {
super(text);
@ -24,7 +25,6 @@ public class MetadataReader extends DocumentParserImpl {
public void parse() {
parseText(text);
OutlineElement lastElement = outline.get(outline.size()-1);
lastElement.setAnnotationEnd(documentEnd);
}
@Override
@ -59,11 +59,14 @@ public class MetadataReader extends DocumentParserImpl {
} catch (WrappedTargetException e) {
e.printStackTrace();
}
if (outlineLevel != null && outlineLevel > 0) {
if (firstPara || (outlineLevel != null && outlineLevel > 0)) {
XTextRange textRange = textContent.getAnchor();
OutlineElement lastElement = outline.get(outline.size()-1);
lastElement.setAnnotationEnd(textContent.getAnchor().getStart());
outline.add(new OutlineElement(textRange));
if (firstPara) {
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();
}
private void writeMetadata() {
private void writeOutlineMetadata() {
cleanMetadataInDocument();
for (OutlineElement element : outline){
Object annotation;
try {
annotation = multiServiceFactory.createInstance("com.sun.star.text.textfield.nnotation");
annotation = multiServiceFactory.createInstance("com.sun.star.text.textfield.annotation");
element.writeMetadata(annotation);
} catch (com.sun.star.uno.Exception e) {
e.printStackTrace();

View file

@ -19,25 +19,38 @@ import com.sun.star.uno.UnoRuntime;
import static pro.litvinovg.libreoffice.metadata.Document.*;
public class OutlineElement {
private String name;
private String elementName;
private ArrayList<MetadataElement> metadata = null;
public String getName() {
return name;
return elementName;
}
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.name = textRange.getString();
this.elementName = name;
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) {
@ -59,24 +72,23 @@ public class OutlineElement {
private String metadataToString() {
JSONObject json = new JSONObject();
for (MetadataElement element : metadata) {
json.put(element.getName(), element.getValue());
String name = element.getName();
String value = element.getValue();
if (!name.isEmpty() && !value.isEmpty()) {
json.put(element.getName(), element.getValue());
}
}
return json.toString();
}
public void writeMetadata(Object annotation) {
XPropertySet properties = UnoRuntime.queryInterface(XPropertySet.class, annotation);
XText startText = annotationStart.getText();
XText startText = textRange.getText();
try {
properties.setPropertyValue("Author", METADATA_EXTENSION);
properties.setPropertyValue("Content", metadataToString());
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);
startText.insertTextContent( annotationRange, textField, true );
} catch (IllegalArgumentException e) {

View file

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