Skip to content
Benjamin Asbach edited this page Oct 28, 2024 · 2 revisions

According to Markus on http://ubion.ion.ag/mainForumFolder/noa_forum/0199?b_start:int=0#0016, this source will create a OpenOffice.org Calc window in a SWING frame

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ag.ion.bion.officelayer.NativeView;
import ag.ion.bion.officelayer.application.IApplicationAssistant;
import ag.ion.bion.officelayer.application.ILazyApplicationInfo;
import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.desktop.IFrame;
import ag.ion.bion.officelayer.document.DocumentDescriptor;
import ag.ion.bion.officelayer.document.IDocument;
import ag.ion.bion.officelayer.internal.application.ApplicationAssistant;
import ag.ion.bion.officelayer.spreadsheet.ISpreadsheetDocument;
import ag.ion.bion.officelayer.text.table.TextTableCellNameHelper;

import com.sun.star.sheet.XSheetCellCursor;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.XCell;
import com.sun.star.text.XText;
import com.sun.star.uno.UnoRuntime;

public class SimpleApp extends JFrame {

  private static final String  CELL_NAME         = "C1";
  private static final String  SHEET_NAME        = "Tabelle1"; //or Table1

  private IOfficeApplication   officeApplication = null;
  private IFrame               officeFrame       = null;
  private ISpreadsheetDocument document          = null;
  private JPanel               noaPanel          = null;
  private JTextField           textField         = null;

  public SimpleApp() {
    super(SimpleApp.class.getName());
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JLabel label = new JLabel("Value: ");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 0, 0, 0);
    c.weightx = 0.1;
    c.gridx = 0;
    c.gridy = 0;
    getContentPane().add(label, c);
    textField = new JTextField();
    label.setLabelFor(textField);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.7;
    c.gridx = 1;
    c.gridy = 0;
    getContentPane().add(textField, c);
    JButton buttonRead = new JButton("Read Value Of " + CELL_NAME);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.1;
    c.gridx = 2;
    c.gridy = 0;
    getContentPane().add(buttonRead, c);

    buttonRead.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          XSpreadsheets sheets = document.getSpreadsheetDocument().getSheets();
          XSpreadsheet spreadsheet1 = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class,
              sheets.getByName(SHEET_NAME));
          XSheetCellCursor cellCursor = spreadsheet1.createCursor();
          XCell cell = cellCursor.getCellByPosition(TextTableCellNameHelper.getColumnIndex(CELL_NAME),
              TextTableCellNameHelper.getRowIndex(CELL_NAME));
          XText cellText = (XText) UnoRuntime.queryInterface(XText.class, cell);
          textField.setText(cellText.getString());
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }
    });

    JButton buttonReadFormula = new JButton("Read Formula Of " + CELL_NAME);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.1;
    c.gridx = 3;
    c.gridy = 0;
    getContentPane().add(buttonReadFormula, c);

    buttonReadFormula.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          XSpreadsheets sheets = document.getSpreadsheetDocument().getSheets();
          XSpreadsheet spreadsheet1 = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class,
              sheets.getByName(SHEET_NAME));
          XSheetCellCursor cellCursor = spreadsheet1.createCursor();
          XCell cell = cellCursor.getCellByPosition(TextTableCellNameHelper.getColumnIndex(CELL_NAME),
              TextTableCellNameHelper.getRowIndex(CELL_NAME));
          String formula = cell.getFormula();
          if (formula != null && formula.startsWith("=")) {
            textField.setText(formula);
          }
          else {
            textField.setText("NO FORMULA");
          }
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }
    });

    JButton buttonUpdate = new JButton("Update Formula/Value Of " + CELL_NAME);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.1;
    c.gridx = 4;
    c.gridy = 0;
    getContentPane().add(buttonUpdate, c);

    buttonUpdate.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          XSpreadsheets sheets = document.getSpreadsheetDocument().getSheets();
          XSpreadsheet spreadsheet1 = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class,
              sheets.getByName(SHEET_NAME));
          XSheetCellCursor cellCursor = spreadsheet1.createCursor();
          XCell cell = cellCursor.getCellByPosition(TextTableCellNameHelper.getColumnIndex(CELL_NAME),
              TextTableCellNameHelper.getRowIndex(CELL_NAME));
          XText cellText = (XText) UnoRuntime.queryInterface(XText.class, cell);
          String valueStr = textField.getText();
          Object valueObj = valueStr;
          try {
            valueObj = Double.valueOf(Double.parseDouble(valueStr));
          }
          catch (NumberFormatException nfe) {
            //nu number
          }
          if (valueObj instanceof Double)
            cell.setValue(((Double) valueObj).doubleValue());
          else if (valueStr.startsWith("="))
            cell.setFormula(valueStr);
          else
            cellText.setString(valueStr);
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }
    });

    JButton buttonNewDoc = new JButton("Open new Document/Close old one");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.1;
    c.gridx = 5;
    c.gridy = 0;
    getContentPane().add(buttonNewDoc, c);

    buttonNewDoc.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          IDocument oldDoc = document;
          document = (ISpreadsheetDocument) officeApplication.getDocumentService().constructNewDocument(officeFrame,
              IDocument.CALC,
              DocumentDescriptor.DEFAULT);
          oldDoc.close();
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }
    });

    noaPanel = new JPanel();
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.insets = new Insets(10, 0, 0, 0);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 6;
    getContentPane().add(noaPanel, c);
    setSize(1024, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    fillNOAPanel();
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent windowEvent) {
        try {
          if (document != null)
            document.close();
          document = null;
          /*if (officeApplication != null) {
            officeApplication.deactivate();
            officeApplication.dispose();
            officeApplication = null;
          }*/
        }
        catch (Exception exception) {
          //do not consume
        }
      }
    });
  }

  private void fillNOAPanel() {
    if (noaPanel != null) {
      try {
        if (officeApplication == null)
          officeApplication = startOOO();
        officeFrame = constructOOOFrame(officeApplication, noaPanel);
        document = (ISpreadsheetDocument) officeApplication.getDocumentService().constructNewDocument(officeFrame,
            IDocument.CALC,
            DocumentDescriptor.DEFAULT);

        noaPanel.setVisible(true);
      }
      catch (Throwable throwable) {
        noaPanel.add(new JLabel("An error occured while creating the NOA panel: " + throwable.getMessage()));
      }
    }
  }

  private IOfficeApplication startOOO() throws Throwable {
    IApplicationAssistant applicationAssistant = new ApplicationAssistant(System.getProperty("user.dir") + "\\lib");
    ILazyApplicationInfo[] appInfos = applicationAssistant.getLocalApplications();
    if (appInfos.length < 1)
      throw new Throwable("No OpenOffice.org Application found.");
    HashMap configuration = new HashMap();
    configuration.put(IOfficeApplication.APPLICATION_HOME_KEY, appInfos[0].getHome());
    configuration.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
    IOfficeApplication officeAplication = OfficeApplicationRuntime.getApplication(configuration);

    officeAplication.setConfiguration(configuration);
    officeAplication.activate();
    return officeAplication;
  }

  private IFrame constructOOOFrame(IOfficeApplication officeApplication, final Container parent)
      throws Throwable {
    final NativeView nativeView = new NativeView(System.getProperty("user.dir") + "\\lib");
    parent.add(nativeView);
    parent.addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent e) {
        nativeView.setPreferredSize(new Dimension(parent.getWidth() - 5, parent.getHeight() - 5));
        parent.getLayout().layoutContainer(parent);
      }
    });
    nativeView.setPreferredSize(new Dimension(parent.getWidth() - 5, parent.getHeight() - 5));
    parent.getLayout().layoutContainer(parent);
    IFrame officeFrame = officeApplication.getDesktopService().constructNewOfficeFrame(nativeView);
    parent.validate();
    return officeFrame;
  }

  public static void main(String[] argv) {
    new SimpleApp();
  }
}
Clone this wiki locally