mainframe.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 174 行

JAVA
174
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.gui.internal;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import org.rapla.client.ClientService;
import org.rapla.entities.User;
import org.rapla.facade.ModificationEvent;
import org.rapla.facade.ModificationListener;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.RaplaGUIComponent;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.internal.common.InternMenus;
import org.rapla.gui.toolkit.DialogUI;
import org.rapla.gui.toolkit.RaplaFrame;



public class MainFrame extends RaplaGUIComponent
    implements
        ModificationListener
{
    RaplaMenuBar menuBar;
    RaplaFrame frame = null;
    Listener listener = new Listener();
    CalendarEditor cal;
    JLabel statusBar = new JLabel();
    public MainFrame(RaplaContext sm) throws RaplaException {
        super(sm);
        menuBar = new RaplaMenuBar(getContext());
        frame = (RaplaFrame) getService( ClientService.MAIN_COMPONENT );
        frame.setTitle(getString("rapla.title"));
        getUpdateModule().addModificationListener(this);
        CalendarSelectionModel model = (CalendarSelectionModel) getService( CalendarSelectionModel.ROLE);
        cal = new CalendarEditor(sm,model);

        JMenuBar menuBar = (JMenuBar) getService( InternMenus.MENU_BAR);
        frame.setJMenuBar( menuBar );
        statusBar.setText("Hello Christopher");

        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add ( statusBar, BorderLayout.SOUTH);

        getContentPane().add(  cal.getComponent() , BorderLayout.CENTER );
    }

    public void show() throws RaplaException {
        getLogger().debug("Creating Main-Frame");
        createFrame();
        //dataChanged(null);
        setStatus();
        cal.start();
        frame.setIconImage(getI18n().getIcon("icon.rapla_small").getImage());
        frame.setVisible(true);
        getFrameList().setMainWindow(frame);
    }

    private JPanel getContentPane() {
        return (JPanel) frame.getContentPane();
    }

    private void createFrame() {
        Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(new Dimension(
                                    Math.min(dimension.width,1000)
                                    ,Math.min(dimension.height-20,780)
                                    )
                      );

        frame.addVetoableChangeListener(listener);
        statusBar.setBorder( BorderFactory.createEtchedBorder());
    }


    class Listener implements VetoableChangeListener {
        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            if (shouldExit())
                close();
            else
                throw new PropertyVetoException("Don't close",evt);
        }

    }

    public Window getFrame() {
        return (Window) frame;
    }

    public JComponent getComponent() {
        return (JComponent) frame.getContentPane();
    }

    private String highlight(String text) {
        return "<FONT color=\"red\">" + text + "</FONT>";
    }

    public void dataChanged(ModificationEvent e) throws RaplaException {
        cal.dataChanged( e );
        setStatus();
    }
    private void setStatus() throws RaplaException{
        User user = getUser();
        String message =
                "<html>"
                + getI18n().format("rapla.welcome",user.getName())
                + " "
                + (user.isAdmin() ?
                        highlight(getString("admin.login")) : ""
                   )
               + "</html>";

        statusBar.setText(message);

    }

    protected boolean shouldExit() {
		try {
	        DialogUI dlg = DialogUI.create(getContext()
				                           ,frame.getRootPane()
				                           ,true
				                           ,getString("exit.title")
				                           ,getString("exit.question")
				                           ,new String[] {
				                               getString("exit.ok")
				                               ,getString("exit.abort")
				                           }
				                           );
			dlg.setIcon(getIcon("icon.question"));
	        //dlg.getButton(0).setIcon(getIcon("icon.confirm"));
	        dlg.getButton(0).setIcon(getIcon("icon.abort"));
	        dlg.setDefault(1);
	        dlg.start();
        return (dlg.getSelectedIndex() == 0);
		} catch (RaplaException e) {
			getLogger().error( e.getMessage(), e);
			return true;
		}

    }

    public void close() {
        getUpdateModule().removeModificationListener(this);
        frame.close();
    }

}





⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?