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

« back to all changes in this revision

Viewing changes to freemind/freemind/view/mindmapview/attributeview/AttributePopupMenu.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-2006 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitri Polivaev and others.
 
3
*
 
4
*See COPYING for Details
 
5
*
 
6
*This program is free software; you can redistribute it and/or
 
7
*modify it under the terms of the GNU General Public License
 
8
*as published by the Free Software Foundation; either version 2
 
9
*of the License, or (at your option) any later version.
 
10
*
 
11
*This program is distributed in the hope that it will be useful,
 
12
*but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
*GNU General Public License for more details.
 
15
*
 
16
*You should have received a copy of the GNU General Public License
 
17
*along with this program; if not, write to the Free Software
 
18
*Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
*/
 
20
/*
 
21
 * Created on 26.11.2005
 
22
 * Copyright (C) 2005 Dimitri Polivaev
 
23
 */
 
24
package freemind.view.mindmapview.attributeview;
 
25
 
 
26
import java.awt.Component;
 
27
import java.awt.EventQueue;
 
28
import java.awt.KeyboardFocusManager;
 
29
import java.awt.Point;
 
30
import java.awt.event.ActionEvent;
 
31
import java.awt.event.ActionListener;
 
32
import java.awt.event.MouseEvent;
 
33
import java.awt.event.MouseListener;
 
34
 
 
35
import javax.swing.JComponent;
 
36
import javax.swing.JMenuItem;
 
37
import javax.swing.JPopupMenu;
 
38
import javax.swing.SwingUtilities;
 
39
import javax.swing.table.JTableHeader;
 
40
 
 
41
import freemind.main.Resources;
 
42
import freemind.modes.attributes.AttributeTableLayoutModel;
 
43
import freemind.modes.attributes.AttributeTableModel;
 
44
 
 
45
/**
 
46
 * @author Dimitri Polivaev
 
47
 * 26.11.2005
 
48
 */
 
49
public class AttributePopupMenu extends JPopupMenu implements MouseListener {
 
50
    private JMenuItem edit = null;
 
51
    private JMenuItem optimalWidth = null;
 
52
    private JMenuItem hide = null;
 
53
    private JMenuItem insert = null;
 
54
    private JMenuItem delete = null;
 
55
    private JMenuItem up = null;
 
56
    private JMenuItem down = null;
 
57
    private AttributeTable table;
 
58
    private int row;
 
59
    
 
60
    /**
 
61
     * @return Returns the optimalWidth.
 
62
     */
 
63
    private JMenuItem getOptimalWidth() {
 
64
        if(optimalWidth == null){
 
65
            optimalWidth = new JMenuItem(Resources.getInstance().getResourceString("attributes_popup_optimal_width"));
 
66
            optimalWidth.addActionListener(new ActionListener(){
 
67
                public void actionPerformed(ActionEvent e) {
 
68
                    table.setOptimalColumnWidths();                    
 
69
                }                
 
70
            });
 
71
        }
 
72
        return optimalWidth;
 
73
    }
 
74
 
 
75
    /**
 
76
     * @return Returns the insert.
 
77
     */
 
78
    private JMenuItem getInsert() {
 
79
        if(insert == null){
 
80
            insert = new JMenuItem(Resources.getInstance().getResourceString("attributes_popup_new"));
 
81
            insert.addActionListener(new ActionListener(){
 
82
                public void actionPerformed(ActionEvent e) {
 
83
                    table.insertRow(row + 1);
 
84
                }                
 
85
            });
 
86
        }
 
87
        return insert;
 
88
    }
 
89
 
 
90
    /**
 
91
     * @return Returns the delete.
 
92
     */
 
93
    private JMenuItem getDelete() {
 
94
        if(delete == null){
 
95
            delete = new JMenuItem(Resources.getInstance().getResourceString("attributes_popup_delete"));
 
96
            delete.addActionListener(new ActionListener(){
 
97
                public void actionPerformed(ActionEvent e) {
 
98
                    table.removeRow(row);                  
 
99
                }                
 
100
            });
 
101
        }
 
102
        return delete;
 
103
    }
 
104
 
 
105
    /**
 
106
     * @return Returns the up.
 
107
     */
 
108
    private JMenuItem getUp() {
 
109
        if(up == null){
 
110
            up = new JMenuItem(Resources.getInstance().getResourceString("attributes_popup_up"));
 
111
            up.addActionListener(new ActionListener(){
 
112
                public void actionPerformed(ActionEvent e) {
 
113
                    table.moveRowUp(row);                    
 
114
                }                
 
115
            });
 
116
        }
 
117
        return up;
 
118
    }
 
119
 
 
120
    /**
 
121
     * @return Returns the down.
 
122
     */
 
123
    private JMenuItem getDown() {
 
124
        if(down == null){
 
125
            down = new JMenuItem(Resources.getInstance().getResourceString("attributes_popup_down"));
 
126
            down.addActionListener(new ActionListener(){
 
127
                public void actionPerformed(ActionEvent e) {
 
128
                    table.moveRowDown(row);                    
 
129
                }                
 
130
            });
 
131
        }
 
132
        return down;
 
133
    }
 
134
    
 
135
   /* (non-Javadoc)
 
136
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
 
137
     */
 
138
    public void mouseClicked(MouseEvent e) {
 
139
    }
 
140
 
 
141
    public void mousePressed(MouseEvent e) {
 
142
        maybeShowPopup(e);
 
143
    }
 
144
 
 
145
    public void mouseReleased(MouseEvent e) {
 
146
        maybeShowPopup(e);
 
147
    }
 
148
 
 
149
    private void maybeShowPopup(MouseEvent e) {
 
150
        if (e.isPopupTrigger()) {
 
151
            selectTable(e.getComponent(), e.getPoint());
 
152
            make();
 
153
            show(e.getComponent(),
 
154
                       e.getX(), e.getY());
 
155
        }
 
156
    }
 
157
 
 
158
    /**
 
159
     * 
 
160
     */
 
161
    private void make() {
 
162
        String attributeViewType = table.getAttributeView().getViewType();
 
163
        AttributeTableModel model = table.getAttributeTableModel();
 
164
        int rowCount = model.getRowCount();
 
165
        if(attributeViewType.equals(AttributeTableLayoutModel.SHOW_ALL)){
 
166
            if(rowCount != 0){
 
167
                add(getOptimalWidth());   
 
168
            }            
 
169
            add(getInsert());
 
170
            if(row != -1){
 
171
                add(getDelete());
 
172
                if(row != 0){
 
173
                    add(getUp());
 
174
                }
 
175
                if(row != rowCount - 1){
 
176
                    add(getDown());
 
177
                }
 
178
            }
 
179
        }
 
180
        else{
 
181
            if(rowCount != 0){
 
182
                add(getOptimalWidth());   
 
183
            }            
 
184
        }
 
185
        
 
186
    }
 
187
 
 
188
    private void selectTable(Component component, Point point) throws AssertionError {
 
189
        int componentCount = getComponentCount();
 
190
        for(int i = componentCount; i > 0;){
 
191
            remove(--i);
 
192
        }
 
193
        if(component instanceof AttributeTable){
 
194
                oldTable = false;
 
195
            table = (AttributeTable)component;  
 
196
            row = table.rowAtPoint(point);
 
197
            if(table.getValueAt(row, 0).equals("")){
 
198
                row--;
 
199
            }
 
200
            int selectedRow = table.getSelectedRow();
 
201
        }
 
202
        else if (component instanceof JTableHeader){
 
203
                oldTable = false;
 
204
            JTableHeader header = (JTableHeader)component;
 
205
            table = (AttributeTable)header.getTable();
 
206
            row = -1;
 
207
        }
 
208
        else{
 
209
            throw new AssertionError();
 
210
        }
 
211
        table.requestFocus();
 
212
    }
 
213
 
 
214
    /* (non-Javadoc)
 
215
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
 
216
     */
 
217
    public void mouseEntered(MouseEvent e) {
 
218
    }
 
219
 
 
220
    /* (non-Javadoc)
 
221
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
 
222
     */
 
223
    public void mouseExited(MouseEvent e) {
 
224
    }
 
225
 
 
226
    private boolean oldTable;
 
227
    protected void firePopupMenuWillBecomeInvisible() {
 
228
        if(row != -1){
 
229
            table.removeRowSelectionInterval(row, row);
 
230
        }
 
231
        oldTable = true;
 
232
        EventQueue.invokeLater(new Runnable(){
 
233
 
 
234
                        public void run() {
 
235
                if(! oldTable){
 
236
                        return;
 
237
                }
 
238
                final KeyboardFocusManager focusManager = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager();
 
239
                final Component focusOwner = SwingUtilities.getAncestorOfClass(AttributeTable.class, focusManager.getFocusOwner());
 
240
                if(table != focusOwner
 
241
                        && focusOwner instanceof JComponent){
 
242
                    table.requestFocus(true);
 
243
                    ((JComponent)focusOwner).requestFocus();
 
244
                }
 
245
                table = null;
 
246
            }
 
247
        });
 
248
    }
 
249
    protected void firePopupMenuWillBecomeVisible() {
 
250
        if(row != -1){
 
251
            table.addRowSelectionInterval(row, row);
 
252
        }
 
253
    }
 
254
 
 
255
    public AttributeTable getTable() {
 
256
        return table;
 
257
    }
 
258
}