001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ruby.debugger.model;
043:
044: import java.io.File;
045: import java.util.List;
046: import java.util.concurrent.CopyOnWriteArrayList;
047: import javax.swing.Action;
048: import org.netbeans.modules.ruby.debugger.ContextProviderWrapper;
049: import org.netbeans.modules.ruby.debugger.EditorUtil;
050: import org.netbeans.modules.ruby.debugger.RubySession;
051: import org.netbeans.spi.debugger.ContextProvider;
052: import org.netbeans.spi.viewmodel.ModelEvent;
053: import org.netbeans.spi.viewmodel.NodeModel;
054: import org.netbeans.spi.viewmodel.TableModel;
055: import org.netbeans.spi.viewmodel.TreeModel;
056: import org.netbeans.spi.viewmodel.ModelListener;
057: import org.netbeans.spi.viewmodel.UnknownTypeException;
058: import org.netbeans.spi.debugger.ui.Constants;
059: import org.netbeans.spi.viewmodel.NodeActionsProvider;
060: import org.openide.util.NbBundle;
061: import org.rubyforge.debugcommons.model.RubyFrame;
062:
063: public class CallStackModel implements TreeModel, NodeModel,
064: NodeActionsProvider, TableModel {
065:
066: public static final String CALL_STACK = "org/netbeans/modules/debugger/resources/callStackView/NonCurrentFrame"; // NOI18N
067: public static final String CURRENT_CALL_STACK = "org/netbeans/modules/debugger/resources/callStackView/CurrentFrame"; // NOI18N
068:
069: private final ContextProviderWrapper contextProvider;
070: private final RubySession rubySession;
071: private final List<ModelListener> listeners;
072:
073: public CallStackModel(final ContextProvider contextProvider) {
074: this .contextProvider = new ContextProviderWrapper(
075: contextProvider);
076: rubySession = this .contextProvider.getRubySession();
077: listeners = new CopyOnWriteArrayList<ModelListener>();
078: }
079:
080: // TreeModel implementation ................................................
081:
082: public Object getRoot() {
083: return ROOT;
084: }
085:
086: public Object[] getChildren(Object parent, int from, int to)
087: throws UnknownTypeException {
088: if (parent == ROOT) {
089: return rubySession.getFrames();
090: } else {
091: throw new UnknownTypeException(parent);
092: }
093: }
094:
095: public boolean isLeaf(Object node) throws UnknownTypeException {
096: if (node == ROOT) {
097: return false;
098: } else if (node instanceof RubyFrame) {
099: return true;
100: } else {
101: throw new UnknownTypeException(node);
102: }
103: }
104:
105: public int getChildrenCount(Object node)
106: throws UnknownTypeException {
107: if (node == ROOT) {
108: return rubySession.getFrames().length;
109: } else {
110: throw new UnknownTypeException(node);
111: }
112: }
113:
114: public void addModelListener(ModelListener l) {
115: listeners.add(l);
116: }
117:
118: public void removeModelListener(ModelListener l) {
119: listeners.remove(l);
120: }
121:
122: public void fireChanges() {
123: for (ModelListener listener : listeners) {
124: listener.modelChanged(new ModelEvent.TreeChanged(this ));
125: }
126: }
127:
128: // NodeModel implementation ................................................
129:
130: public String getDisplayName(Object node)
131: throws UnknownTypeException {
132: if (node == ROOT) {
133: return NbBundle.getMessage(CallStackModel.class,
134: "CTL_CallstackModel.Column.Name.Name");
135: } else if (node instanceof RubyFrame) {
136: RubyFrame frame = ((RubyFrame) node);
137: String basename = new File(frame.getFile()).getName();
138: String dn = basename + ':' + frame.getLine();
139: return rubySession.isSelectedFrame(frame) ? "<html><b>"
140: + dn + "</b></html>" : dn; // NOI18N
141: } else {
142: throw new UnknownTypeException(node);
143: }
144: }
145:
146: public String getIconBase(Object node) throws UnknownTypeException {
147: if (node instanceof RubyFrame) {
148: if (rubySession.isSelectedFrame((RubyFrame) node)) {
149: return CURRENT_CALL_STACK;
150: } else {
151: return CALL_STACK;
152: }
153: } else if (node == ROOT) {
154: return null;
155: } else {
156: throw new UnknownTypeException(node);
157: }
158: }
159:
160: public String getShortDescription(Object node)
161: throws UnknownTypeException {
162: if (node == ROOT) {
163: return NbBundle.getMessage(CallStackModel.class,
164: "CTL_CallstackModel.Column.Name.Desc");
165: } else if (node instanceof RubyFrame) {
166: return ((RubyFrame) node).getName();
167: } else {
168: throw new UnknownTypeException(node);
169: }
170: }
171:
172: // NodeActionsProvider implementation ......................................
173:
174: public void performDefaultAction(Object node)
175: throws UnknownTypeException {
176: if (node instanceof RubyFrame) {
177: RubyFrame frame = (RubyFrame) node;
178: EditorUtil.showLine(EditorUtil.getLineAnnotable(rubySession
179: .resolveAbsolutePath(frame.getFile()), frame
180: .getLine() - 1));
181: rubySession.selectFrame(frame);
182: fireChanges();
183: contextProvider.getVariablesModel().fireChanges();
184: contextProvider.getWatchesModel().fireChanges();
185: } else {
186: throw new UnknownTypeException(node);
187: }
188: }
189:
190: public Action[] getActions(Object node) throws UnknownTypeException {
191: return new Action[] {};
192: }
193:
194: // TableModel implementation ...............................................
195:
196: public Object getValueAt(Object node, String columnID)
197: throws UnknownTypeException {
198: if (columnID == Constants.CALL_STACK_FRAME_LOCATION_COLUMN_ID
199: && node instanceof RubyFrame) {
200: RubyFrame frame = (RubyFrame) node;
201: return frame.getFile();
202: } else {
203: throw new UnknownTypeException(node);
204: }
205: }
206:
207: public boolean isReadOnly(Object node, String columnID)
208: throws UnknownTypeException {
209: if (columnID == Constants.CALL_STACK_FRAME_LOCATION_COLUMN_ID
210: && node instanceof RubyFrame) {
211: return true;
212: } else {
213: throw new UnknownTypeException(node);
214: }
215: }
216:
217: public void setValueAt(Object node, String columnID, Object value)
218: throws UnknownTypeException {
219: throw new UnknownTypeException(node);
220: }
221:
222: }
|