001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.internal;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Dimension;
017: import java.awt.Window;
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyVetoException;
020: import java.beans.VetoableChangeListener;
021:
022: import javax.swing.BorderFactory;
023: import javax.swing.JComponent;
024: import javax.swing.JLabel;
025: import javax.swing.JMenuBar;
026: import javax.swing.JPanel;
027:
028: import org.rapla.client.ClientService;
029: import org.rapla.entities.User;
030: import org.rapla.facade.ModificationEvent;
031: import org.rapla.facade.ModificationListener;
032: import org.rapla.framework.RaplaContext;
033: import org.rapla.framework.RaplaException;
034: import org.rapla.gui.RaplaGUIComponent;
035: import org.rapla.gui.internal.common.CalendarSelectionModel;
036: import org.rapla.gui.internal.common.InternMenus;
037: import org.rapla.gui.toolkit.DialogUI;
038: import org.rapla.gui.toolkit.RaplaFrame;
039:
040: public class MainFrame extends RaplaGUIComponent implements
041: ModificationListener {
042: RaplaMenuBar menuBar;
043: RaplaFrame frame = null;
044: Listener listener = new Listener();
045: CalendarEditor cal;
046: JLabel statusBar = new JLabel();
047:
048: public MainFrame(RaplaContext sm) throws RaplaException {
049: super (sm);
050: menuBar = new RaplaMenuBar(getContext());
051: frame = (RaplaFrame) getService(ClientService.MAIN_COMPONENT);
052: frame.setTitle(getString("rapla.title"));
053: getUpdateModule().addModificationListener(this );
054: CalendarSelectionModel model = (CalendarSelectionModel) getService(CalendarSelectionModel.ROLE);
055: cal = new CalendarEditor(sm, model);
056:
057: JMenuBar menuBar = (JMenuBar) getService(InternMenus.MENU_BAR);
058: frame.setJMenuBar(menuBar);
059: statusBar.setText("Hello Christopher");
060:
061: getContentPane().setLayout(new BorderLayout());
062: getContentPane().add(statusBar, BorderLayout.SOUTH);
063:
064: getContentPane().add(cal.getComponent(), BorderLayout.CENTER);
065: }
066:
067: public void show() throws RaplaException {
068: getLogger().debug("Creating Main-Frame");
069: createFrame();
070: //dataChanged(null);
071: setStatus();
072: cal.start();
073: frame.setIconImage(getI18n().getIcon("icon.rapla_small")
074: .getImage());
075: frame.setVisible(true);
076: getFrameList().setMainWindow(frame);
077: }
078:
079: private JPanel getContentPane() {
080: return (JPanel) frame.getContentPane();
081: }
082:
083: private void createFrame() {
084: Dimension dimension = java.awt.Toolkit.getDefaultToolkit()
085: .getScreenSize();
086: frame.setSize(new Dimension(Math.min(dimension.width, 1000),
087: Math.min(dimension.height - 20, 780)));
088:
089: frame.addVetoableChangeListener(listener);
090: statusBar.setBorder(BorderFactory.createEtchedBorder());
091: }
092:
093: class Listener implements VetoableChangeListener {
094: public void vetoableChange(PropertyChangeEvent evt)
095: throws PropertyVetoException {
096: if (shouldExit())
097: close();
098: else
099: throw new PropertyVetoException("Don't close", evt);
100: }
101:
102: }
103:
104: public Window getFrame() {
105: return (Window) frame;
106: }
107:
108: public JComponent getComponent() {
109: return (JComponent) frame.getContentPane();
110: }
111:
112: private String highlight(String text) {
113: return "<FONT color=\"red\">" + text + "</FONT>";
114: }
115:
116: public void dataChanged(ModificationEvent e) throws RaplaException {
117: cal.dataChanged(e);
118: setStatus();
119: }
120:
121: private void setStatus() throws RaplaException {
122: User user = getUser();
123: String message = "<html>"
124: + getI18n().format("rapla.welcome", user.getName())
125: + " "
126: + (user.isAdmin() ? highlight(getString("admin.login"))
127: : "") + "</html>";
128:
129: statusBar.setText(message);
130:
131: }
132:
133: protected boolean shouldExit() {
134: try {
135: DialogUI dlg = DialogUI.create(getContext(), frame
136: .getRootPane(), true, getString("exit.title"),
137: getString("exit.question"), new String[] {
138: getString("exit.ok"),
139: getString("exit.abort") });
140: dlg.setIcon(getIcon("icon.question"));
141: //dlg.getButton(0).setIcon(getIcon("icon.confirm"));
142: dlg.getButton(0).setIcon(getIcon("icon.abort"));
143: dlg.setDefault(1);
144: dlg.start();
145: return (dlg.getSelectedIndex() == 0);
146: } catch (RaplaException e) {
147: getLogger().error(e.getMessage(), e);
148: return true;
149: }
150:
151: }
152:
153: public void close() {
154: getUpdateModule().removeModificationListener(this);
155: frame.close();
156: }
157:
158: }
|