001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Johann Gyger <jgyger@users.sf.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.views;
024:
025: import java.util.Vector;
026:
027: import net.sf.jmoney.IBookkeepingPage;
028: import net.sf.jmoney.IBookkeepingPageFactory;
029: import net.sf.jmoney.JMoneyPlugin;
030: import net.sf.jmoney.model2.Account;
031: import net.sf.jmoney.model2.AccountInfo;
032: import net.sf.jmoney.model2.ExtendableObject;
033: import net.sf.jmoney.model2.ExtendablePropertySet;
034: import net.sf.jmoney.model2.PageEntry;
035: import net.sf.jmoney.model2.PropertySet;
036: import net.sf.jmoney.model2.ScalarPropertyAccessor;
037: import net.sf.jmoney.model2.SessionChangeAdapter;
038: import net.sf.jmoney.model2.SessionChangeListener;
039:
040: import org.eclipse.core.runtime.IProgressMonitor;
041: import org.eclipse.ui.IEditorInput;
042: import org.eclipse.ui.IEditorSite;
043: import org.eclipse.ui.IMemento;
044: import org.eclipse.ui.IWorkbenchWindow;
045: import org.eclipse.ui.PartInitException;
046: import org.eclipse.ui.actions.ActionGroup;
047: import org.eclipse.ui.forms.editor.FormEditor;
048: import org.eclipse.ui.operations.UndoRedoActionGroup;
049:
050: /**
051: * TODO
052: *
053: * @author Johann Gyger
054: */
055: public class NodeEditor extends FormEditor {
056:
057: protected Object navigationTreeNode;
058: protected Vector<PageEntry> pageListeners;
059:
060: protected SessionChangeListener accountNameChangeListener = null;
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
064: */
065: @Override
066: protected void addPages() {
067: NodeEditorInput cInput = (NodeEditorInput) this
068: .getEditorInput();
069: IMemento memento = cInput.getMemento();
070:
071: IBookkeepingPage pages[] = new IBookkeepingPage[pageListeners
072: .size()];
073:
074: for (int i = 0; i < pageListeners.size(); i++) {
075: PageEntry entry = pageListeners.get(i);
076: String pageId = entry.getPageId();
077: IBookkeepingPageFactory pageListener = entry
078: .getPageFactory();
079: pages[i] = pageListener.createFormPage(this ,
080: memento == null ? null : memento.getChild(pageId));
081: }
082:
083: cInput.pages = pages;
084: }
085:
086: @Override
087: public void dispose() {
088: // When the editor is disposed, let the editor input
089: // know that there is no editor open. This is necessary
090: // because the input is kept around by Eclipse even when the
091: // editor is closed and kept in a Most Recently Used list.
092: // This list is persisted in the workbench memento, but the
093: // editor input must not try to persist the values of the controls
094: // because the controls no longer exist.
095: // We indicate this situation by clearing out the list
096: // of pages.
097: NodeEditorInput cInput = (NodeEditorInput) this
098: .getEditorInput();
099: cInput.pages = null;
100:
101: if (accountNameChangeListener != null) {
102: ((Account) cInput.getNode()).getDataManager()
103: .removeChangeListener(accountNameChangeListener);
104: }
105:
106: super .dispose();
107: }
108:
109: /* (non-Javadoc)
110: * @see org.eclipse.ui.IEditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
111: */
112: @Override
113: public void init(IEditorSite site, IEditorInput input)
114: throws PartInitException {
115: super .init(site, input);
116:
117: final NodeEditorInput cInput = (NodeEditorInput) input;
118:
119: navigationTreeNode = cInput.getNode();
120: pageListeners = cInput.getPageListeners();
121:
122: setPartName(cInput.getName());
123: setTitleImage(cInput.getImage());
124:
125: /*
126: * If the node object is an account then the title is the name of the account.
127: * We must listen for changes in the name and update the title accordingly.
128: * This ensures the title changes when the account name is edited.
129: */
130: if (cInput.getNode() instanceof Account) {
131: final Account account = (Account) cInput.getNode();
132: accountNameChangeListener = new SessionChangeAdapter() {
133: @Override
134: public void objectChanged(
135: ExtendableObject changedObject,
136: ScalarPropertyAccessor propertyAccessor,
137: Object oldValue, Object newValue) {
138: if (changedObject == account
139: && propertyAccessor == AccountInfo
140: .getNameAccessor()) {
141: setPartName(account.getName());
142: }
143: }
144: };
145: account.getDataManager().addChangeListener(
146: accountNameChangeListener);
147: }
148:
149: ActionGroup ag = new UndoRedoActionGroup(
150: // getSite(),
151: site, getSite().getWorkbenchWindow().getWorkbench()
152: .getOperationSupport().getUndoContext(), true);
153: ag.fillActionBars(site.getActionBars());
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
158: */
159: @Override
160: public void doSave(IProgressMonitor monitor) {
161: // TODO Auto-generated method stub
162: }
163:
164: /* (non-Javadoc)
165: * @see org.eclipse.ui.ISaveablePart#doSaveAs()
166: */
167: @Override
168: public void doSaveAs() {
169: throw new RuntimeException("Illegal invocation");
170: }
171:
172: /* (non-Javadoc)
173: * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
174: */
175: @Override
176: public boolean isSaveAsAllowed() {
177: return false;
178: }
179:
180: /**
181: * @return
182: */
183: public Object getSelectedObject() {
184: return navigationTreeNode;
185: }
186:
187: /**
188: * @param extendableObject
189: */
190: public static void openEditor(IWorkbenchWindow window,
191: ExtendableObject extendableObject) {
192: ExtendablePropertySet<?> propertySet = PropertySet
193: .getPropertySet(extendableObject.getClass());
194: Vector<PageEntry> pages = propertySet.getPageFactories();
195:
196: // Create an editor for this node (or active if an editor
197: // is already open). However, if no pages are registered for this
198: // node then do nothing.
199: if (!pages.isEmpty()) {
200: try {
201: IEditorInput editorInput = new NodeEditorInput(
202: extendableObject, extendableObject.toString(),
203: propertySet.getIcon(), pages, null);
204: window.getActivePage().openEditor(editorInput,
205: "net.sf.jmoney.genericEditor");
206: } catch (PartInitException e) {
207: JMoneyPlugin.log(e);
208: }
209: }
210: }
211: }
|