import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.sun.star.view.DocumentZoomType;
import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationException;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.desktop.DesktopException;
import ag.ion.bion.officelayer.desktop.IFrame;
import ag.ion.bion.officelayer.document.DocumentDescriptor;
import ag.ion.bion.officelayer.document.DocumentException;
import ag.ion.bion.officelayer.text.IParagraph;
import ag.ion.bion.officelayer.text.ITextCursor;
import ag.ion.bion.officelayer.text.ITextDocument;
import ag.ion.bion.officelayer.text.TextException;
import ag.ion.noa.NOAException;
public class OfficeFrame
extends JFrame
{
private ListPanel listPanel;
private OfficePanel officePanel;
public OfficeFrame()
{
officePanel = new OfficePanel();
add(officePanel,BorderLayout.CENTER);
listPanel = new ListPanel(officePanel);
add(listPanel,BorderLayout.NORTH);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
addWindowStateListener(new WindowStateListener()
{
public void windowStateChanged(WindowEvent e)
{
if(e.getNewState()==WindowEvent.WINDOW_CLOSED);
{
officePanel.terminate();
System.exit(0);
}
}
});
}
/**
* @param args
*/
public static void main(String[] args)
{
OfficeFrame frame = new OfficeFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class ListPanel extends JPanel
{
private DefaultListModel model;
private JList list;
private OfficePanel officePanel;
public ListPanel(OfficePanel officePanel)
{
this.officePanel = officePanel;
setPreferredSize(new Dimension(800,100));
setLayout(new BorderLayout());
model = new DefaultListModel();
list = new JList(model);
list.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
ListPanel.this.officePanel.showDocument(String.valueOf(list.getSelectedIndex()));
}
});
add(list,BorderLayout.CENTER);
JButton add = new JButton("add");
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
model.addElement(String.valueOf(model.getSize()+1));
}
});
add(add,BorderLayout.EAST);
}
}
class OfficePanel extends JPanel
{
private CardLayout layout;
private IOfficeApplication application;
private Set frames = new HashSet();
public OfficePanel()
{
layout = new CardLayout();
setLayout(layout);
}
public void showDocument(String title)
{
if(!frames.contains(title))
createOfficeFrame(title);
layout.show(this, title);
}
private void createOfficeFrame(String title)
{
if(application==null)
startApplication();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,1));
add(panel,title);
frames.add(title);
try
{
IFrame frame = application.getDesktopService().constructNewOfficeFrame(panel);
ITextDocument doc = (ITextDocument)application.getDocumentService().constructNewDocument(frame,title, DocumentDescriptor.DEFAULT);
doc.zoom(DocumentZoomType.OPTIMAL, (short)0);
ITextCursor cursor = doc.getTextService().getCursorService().getTextCursor();
IParagraph para = doc.getTextService().getTextContentService().constructNewParagraph();
doc.getTextService().getTextContentService().insertTextContent(cursor.getStart(),para);
para.getParagraphProperties().setParaStyleName("Title");
para.setParagraphText("Hallo, ich bin Dokument "+title);
}
catch (DesktopException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (OfficeApplicationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NOAException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (TextException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
validate();
}
private void startApplication()
{
try
{
String path = OfficeApplicationRuntime.getApplicationAssistant().getLocalApplications()[0].getHome();
Map config = new HashMap();
config.put(IOfficeApplication.APPLICATION_HOME_KEY, path);
config.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
application = OfficeApplicationRuntime.getApplication(config);
application.activate();
}
catch (OfficeApplicationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void terminate()
{
application.dispose();
}
}