001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lgantt.view;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Dimension;
020: import java.io.File;
021: import java.util.Hashtable;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.swing.JFrame;
026: import javax.swing.JMenu;
027: import javax.swing.JMenuBar;
028: import javax.swing.JMenuItem;
029: import javax.swing.KeyStroke;
030:
031: import com.csa.lgantt.LGanttMain;
032: import com.csa.lgantt.MenuBuilder;
033: import com.csa.lgantt.Messages;
034: import com.csa.lgantt.model.IOManager;
035: import com.csa.lgantt.model.Project;
036: import com.csa.lgantt.model.ProjectChange;
037: import com.csa.lgantt.model.ProjectListener;
038: import com.csa.lgantt.view.adapters.ProjectViewModel;
039: import com.csa.lgantt.view.adapters.ProjectViewModelChange;
040: import com.csa.lgantt.view.adapters.ProjectViewModelListener;
041:
042: /**
043: * MainFrame, ventana principal
044: * <p>
045: * $Date: 2006/09/14 08:14:17 $
046: * </p>
047: *
048: * @version $Revision: 1.1 $
049: * @author Carlos Silva
050: */
051: public class MainFrame extends JFrame implements ProjectListener,
052: ProjectViewModelListener {
053:
054: private static final long serialVersionUID = 1L;
055:
056: String WND_TITLE = Messages.getString("MainFrame.title");
057:
058: // / Main display
059: GanttDisplay ganttDisplay;
060:
061: // / ViewModel
062: ProjectViewModel pvModel = null;
063:
064: /**
065: * Acceso a los menues segun su nombre. Relaciona el string del commando con
066: * el JMenuItem
067: */
068: protected Hashtable menues = new Hashtable();
069:
070: /**
071: * Crea una ventana para editar un proyecto nuevo
072: */
073: public MainFrame() {
074: this (IOManager.createEmptyProject(), null);
075: }
076:
077: /**
078: * Crea una ventana para editar un proyecto existente almacenado en un
079: * archivo. Crear el display y asigna los listener.
080: */
081: public MainFrame(Project project, File file) {
082: super ("");
083: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
084:
085: pvModel = new ProjectViewModel(project, file);
086: ganttDisplay = new GanttDisplay(pvModel);
087:
088: GanttActionListener mainListener = pvModel
089: .getMainActionListener();
090:
091: mainListener.setup(this , ganttDisplay);
092:
093: getContentPane().add(ganttDisplay, BorderLayout.CENTER);
094:
095: JMenuBar menuBar = MenuBuilder.buildMenuBar(mainListener,
096: "menubar", menues);
097: setJMenuBar(menuBar);
098:
099: JMenu menu = (JMenu) menuBar.getComponent(0);
100: menu.addSeparator();
101: List list = LGanttMain.getLastUsed();
102: for (int i = 0; i < list.size(); i++) {
103: File fn = (File) list.get(i);
104: String id = String.valueOf(i + 1);
105: JMenuItem mi = new JMenuItem(id);
106: mi.setActionCommand("lru." + id);
107: mi.setText(fn.getAbsolutePath());
108: mi.setMnemonic(fn.getAbsolutePath().charAt(0));
109: mi.addActionListener(mainListener);
110: menu.add(mi);
111: menues.put(id, mi);
112: }
113:
114: updateUndoRedo();
115:
116: pvModel.addListener(this );
117: project.addListener(this );
118:
119: setSize(new Dimension(850, 500));
120: }
121:
122: /**
123: * Obtiene una referencia al menu que representa un comando
124: *
125: * @param cmd
126: * @return
127: */
128: public JMenuItem getMenuItemForCommand(String cmd) {
129: return (JMenuItem) menues.get(cmd);
130: }
131:
132: /**
133: * Cuando hay un nuevo proyecto cargado... se debe reasignar el listener
134: *
135: * @see com.csa.lgantt.view.adapters.ProjectViewModelListener#viewModelChanged(com.csa.lgantt.view.adapters.ProjectViewModelChange)
136: */
137: public void viewModelChanged(ProjectViewModelChange c) {
138: if (c.getId() == ProjectViewModelChange.NEW_PROJECT_LOADED)
139: c.getProjectViewModel().getProject().addListener(this );
140: }
141:
142: /**
143: * Cuando cambia el proyecto se debe habilitar/deshabilitar undo y redo
144: *
145: * @see com.csa.lgantt.model.ProjectListener#projectChanged(com.csa.lgantt.model.ProjectChange)
146: */
147: public void projectChanged(ProjectChange c) {
148: updateUndoRedo();
149: }
150:
151: /**
152: * Habilita o deshabilita los menues de undo y redo
153: */
154: void updateUndoRedo() {
155: Project project = pvModel.getProject();
156:
157: JMenuItem undoItem = getMenuItemForCommand(GanttActionListener.CMD_EDIT_UNDO);
158: undoItem.setEnabled(!project.isUndoEmpty());
159:
160: updWindowTitle();
161:
162: JMenuItem redoItem = getMenuItemForCommand(GanttActionListener.CMD_EDIT_REDO);
163: redoItem.setEnabled(!project.isRedoEmpty());
164: }
165:
166: /**
167: * Cambia el titulo de la ventana
168: */
169: public void updWindowTitle() {
170: String fn;
171: if (pvModel.getFile() != null)
172: fn = pvModel.getFile().getName();
173: else
174: fn = "(not saved)";
175:
176: String t = pvModel.getProject().getName() + " - " + fn + " - "
177: + WND_TITLE;
178:
179: if (!pvModel.getProject().isUndoEmpty())
180: t = t + " * ";
181:
182: if (!t.equals(getTitle()))
183: setTitle(t);
184: }
185: }
|