~ubuntu-branches/ubuntu/utopic/freemind/utopic

« back to all changes in this revision

Viewing changes to freemind/freemind/view/mindmapview/NodeViewFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2010-01-03 14:19:19 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100103141919-m5az7dkicy21hqop
Tags: 0.9.0~rc6+dfsg-1ubuntu1
* Merge from Debian unstable (LP: #182927), remaining changes:
  - debian/copyright: add license/copyright for
    freemind/freemind/main/ExampleFileFilter.java

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*FreeMind - A Program for creating and viewing Mindmaps
 
2
 *Copyright (C) 2000-2007  Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitri Polivaev and others.
 
3
 *See COPYING for Details
 
4
 *
 
5
 *This program is free software; you can redistribute it and/or
 
6
 *modify it under the terms of the GNU General Public License
 
7
 *as published by the Free Software Foundation; either version 2
 
8
 *of the License, or (at your option) any later version.
 
9
 *
 
10
 *This program is distributed in the hope that it will be useful,
 
11
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *GNU General Public License for more details.
 
14
 *
 
15
 *You should have received a copy of the GNU General Public License
 
16
 *along with this program; if not, write to the Free Software
 
17
 *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
/* $Id: NodeViewFactory.java,v 1.1.4.9 2008/07/21 21:57:51 dpolivaev Exp $ */
 
20
package freemind.view.mindmapview;
 
21
 
 
22
import java.awt.Component;
 
23
import java.awt.Container;
 
24
import java.awt.Dimension;
 
25
import java.awt.Graphics;
 
26
import java.awt.Graphics2D;
 
27
import java.awt.Insets;
 
28
import java.awt.LayoutManager;
 
29
import java.awt.RenderingHints;
 
30
 
 
31
import javax.swing.Box;
 
32
import javax.swing.JComponent;
 
33
 
 
34
import freemind.modes.EdgeAdapter;
 
35
import freemind.modes.MindMapNode;
 
36
 
 
37
class NodeViewFactory {
 
38
        
 
39
        private static class ContentPane extends JComponent{
 
40
                static private LayoutManager layoutManager = new ContentPaneLayout();
 
41
 
 
42
                ContentPane(){
 
43
                        setLayout(layoutManager );
 
44
                }
 
45
        }
 
46
        
 
47
        private static class ContentPaneLayout implements LayoutManager{
 
48
        
 
49
                public void addLayoutComponent(String name, Component comp) {
 
50
                }
 
51
        
 
52
                public void layoutContainer(Container parent) {
 
53
                        final int componentCount = parent.getComponentCount();
 
54
                        final int width = parent.getWidth();
 
55
                        int y = 0;
 
56
                        for(int i = 0; i < componentCount; i++){
 
57
                                final Component component = parent.getComponent(i);
 
58
                                if(component.isVisible()){
 
59
                                        final Dimension preferredCompSize = component.getPreferredSize();
 
60
                                        if(component instanceof MainView){
 
61
                                                component.setBounds(0, y, width, preferredCompSize.height);
 
62
                                        }
 
63
                                        else{
 
64
                                                int x = (int)(component.getAlignmentX() * (width - preferredCompSize.width));
 
65
                                                component.setBounds(x, y, preferredCompSize.width, preferredCompSize.height);
 
66
                                        }
 
67
                                        y += preferredCompSize.height;
 
68
                                }
 
69
                        }
 
70
                }
 
71
        
 
72
                public Dimension minimumLayoutSize(Container parent) {
 
73
                        return preferredLayoutSize(parent);
 
74
                }
 
75
        
 
76
                public Dimension preferredLayoutSize(Container parent) {
 
77
                        final Dimension prefSize = new Dimension(0, 0);
 
78
                        final int componentCount = parent.getComponentCount();
 
79
                        for(int i = 0; i < componentCount; i++){
 
80
                                final Component component = parent.getComponent(i);
 
81
                                if(component.isVisible()){
 
82
                                        final Dimension preferredCompSize = component.getPreferredSize();
 
83
                                        prefSize.height += preferredCompSize.height;
 
84
                                        prefSize.width = Math.max(prefSize.width, preferredCompSize.width);
 
85
                                }
 
86
                        }
 
87
                        return prefSize;
 
88
                }
 
89
        
 
90
                public void removeLayoutComponent(Component comp) {
 
91
                }
 
92
                
 
93
        }
 
94
 
 
95
    private static NodeViewFactory factory;
 
96
    private EdgeView sharpBezierEdgeView;
 
97
    private EdgeView sharpLinearEdgeView;
 
98
    private EdgeView bezierEdgeView;
 
99
    private EdgeView linearEdgeView;
 
100
 
 
101
    //Singleton
 
102
    private NodeViewFactory(){
 
103
        
 
104
    }
 
105
    
 
106
    
 
107
    static NodeViewFactory getInstance(){
 
108
        if(factory == null){
 
109
            factory = new NodeViewFactory();
 
110
        }
 
111
        return factory;
 
112
    }
 
113
    
 
114
    EdgeView getEdge(NodeView newView) {
 
115
        final String edgeStyle = newView.getModel().getEdge().getStyle();
 
116
                if (edgeStyle.equals(EdgeAdapter.EDGESTYLE_LINEAR)) {
 
117
            return getLinearEdgeView();
 
118
        } else if (edgeStyle.equals(EdgeAdapter.EDGESTYLE_BEZIER)) {
 
119
            return getBezierEdgeView();
 
120
        } else if (edgeStyle.equals(EdgeAdapter.EDGESTYLE_SHARP_LINEAR)) {
 
121
            return getSharpEdgeView();
 
122
        } else if (edgeStyle.equals(EdgeAdapter.EDGESTYLE_SHARP_BEZIER)) {
 
123
            return getSharpBezierEdgeView();
 
124
        } else {
 
125
            System.err.println("Unknown Edge Type.");
 
126
            return getLinearEdgeView();
 
127
        }
 
128
    }
 
129
 
 
130
    private EdgeView getSharpBezierEdgeView() {
 
131
        if(sharpBezierEdgeView == null){
 
132
            sharpBezierEdgeView = new SharpBezierEdgeView();
 
133
        }
 
134
        return sharpBezierEdgeView;
 
135
    }
 
136
 
 
137
    private EdgeView getSharpEdgeView() {
 
138
        if(sharpLinearEdgeView == null){
 
139
            sharpLinearEdgeView = new SharpLinearEdgeView();
 
140
        }
 
141
        return sharpLinearEdgeView;
 
142
    }
 
143
 
 
144
    private EdgeView getBezierEdgeView() {
 
145
        if(bezierEdgeView == null){
 
146
            bezierEdgeView = new BezierEdgeView();
 
147
        }
 
148
        return bezierEdgeView;
 
149
    }
 
150
 
 
151
    private EdgeView getLinearEdgeView() {
 
152
        if(linearEdgeView == null){
 
153
            linearEdgeView = new LinearEdgeView();
 
154
        }
 
155
        return linearEdgeView;
 
156
    }
 
157
 
 
158
 
 
159
    /**
 
160
     * Factory method which creates the right NodeView for the model.
 
161
     */
 
162
    NodeView newNodeView(MindMapNode model, int position, MapView map, Container parent) {
 
163
        NodeView newView = new NodeView( model, position, map, parent);
 
164
        
 
165
        if (model.isRoot()) {
 
166
                final MainView mainView = new RootMainView(); 
 
167
            newView.setMainView(mainView);
 
168
            newView.setLayout(VerticalRootNodeViewLayout.getInstance());
 
169
            
 
170
        } else { 
 
171
                    newView.setMainView(newMainView(model));
 
172
            if(newView.isLeft()){
 
173
                newView.setLayout(LeftNodeViewLayout.getInstance());
 
174
            }
 
175
            else{
 
176
                newView.setLayout(RightNodeViewLayout.getInstance());
 
177
            }
 
178
        }
 
179
        
 
180
        model.addViewer(newView);
 
181
        newView.update();
 
182
        fireNodeViewCreated(newView);
 
183
        return newView;
 
184
    }
 
185
 
 
186
 
 
187
        MainView newMainView(MindMapNode model) {
 
188
                if (model.isRoot()) {
 
189
                return new RootMainView();             
 
190
        } 
 
191
                if (model.getStyle().equals(MindMapNode.STYLE_FORK) ){
 
192
                        return new ForkMainView(); 
 
193
                }
 
194
                else if (model.getStyle().equals(MindMapNode.STYLE_BUBBLE)) {
 
195
                        return new BubbleMainView(); 
 
196
                }
 
197
                else {
 
198
                    System.err.println("Tried to create a NodeView of unknown Style.");
 
199
                    return new ForkMainView(); 
 
200
                }
 
201
        }
 
202
 
 
203
 
 
204
    private void fireNodeViewCreated(NodeView newView) {
 
205
                newView.getMap().getModel().getModeController().onViewCreatedHook(newView);             
 
206
        }
 
207
 
 
208
 
 
209
        JComponent newContentPane(NodeView view) {
 
210
        return new ContentPane();
 
211
    }
 
212
}