Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / LayoutKeyEvent.java @ 36648

History | View | Annotate | Download (7.93 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout;
23

    
24
import java.awt.KeyEventDispatcher;
25
import java.awt.event.KeyEvent;
26
import java.awt.geom.Rectangle2D;
27

    
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.messages.NotificationManager;
30
import org.gvsig.andami.ui.mdiManager.IWindow;
31
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
32
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
33
import org.gvsig.tools.undo.RedoException;
34
import org.gvsig.tools.undo.UndoException;
35

    
36
public class LayoutKeyEvent implements KeyEventDispatcher {
37

    
38
    private static IFFrame[] selectFFrames = new IFFrame[0];
39
    private int difX;
40
    private int difY;
41

    
42
    public static boolean copy(LayoutPanel layout)
43
        throws CloneNotSupportedException {
44
        IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
45
        if (fframes.length == 0)
46
            return false;
47
        selectFFrames = new IFFrame[fframes.length];
48
        for (int i = 0; i < fframes.length; i++) {
49
            selectFFrames[i] = (IFFrame) fframes[i].clone();
50
        }
51
        return true;
52
    }
53

    
54
    public static boolean cut(LayoutPanel layout)
55
        throws CloneNotSupportedException {
56
        IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
57
        if (fframes.length == 0)
58
            return false;
59
        selectFFrames = new IFFrame[fframes.length];
60
        for (int i = 0; i < fframes.length; i++) {
61
            selectFFrames[i] = (IFFrame) fframes[i].clone();
62
        }
63
        layout.getLayoutContext().delFFrameSelected();
64
        layout.getLayoutContext().callLayoutDrawListeners();
65
        return true;
66
    }
67

    
68
    public static boolean paste(LayoutPanel layout)
69
        throws CloneNotSupportedException {
70
        IFFrame copyFFrame = null;
71
        layout.getLayoutContext().getFrameCommandsRecord()
72
            .startComplex(PluginServices.getText(layout, "paste_elements"));
73
        for (int i = 0; i < selectFFrames.length; i++) {
74
            copyFFrame = (IFFrame) selectFFrames[i].clone();
75
            if (i == 0)
76
                layout.getLayoutContext().addFFrame(copyFFrame, true, true);
77
            else
78
                layout.getLayoutContext().addFFrame(copyFFrame, false, true);
79
        }
80
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
81
        layout.getLayoutContext().callLayoutDrawListeners();
82
        return true;
83
    }
84

    
85
    public static boolean undo(LayoutPanel layout) {
86
        LayoutContext lcontext = layout.getLayoutContext();
87
        try {
88
            lcontext.getFrameCommandsRecord().undo();
89
        } catch (UndoException e) {
90
            e.printStackTrace();
91
        }
92
        layout.getLayoutContext().updateFFrames();
93
        layout.getLayoutContext().callLayoutDrawListeners();
94
        PluginServices.getMainFrame().enableControls();
95
        return true;
96
    }
97

    
98
    public static boolean redo(LayoutPanel layout) {
99
        LayoutContext lcontext = layout.getLayoutContext();
100
        try {
101
            lcontext.getFrameCommandsRecord().redo();
102
        } catch (RedoException e) {
103
            e.printStackTrace();
104
        }
105
        lcontext.updateFFrames();
106
        lcontext.callLayoutDrawListeners();
107
        PluginServices.getMainFrame().enableControls();
108
        return true;
109
    }
110

    
111
    public boolean dispatchKeyEvent(KeyEvent e) {
112
        IWindow view = PluginServices.getMDIManager().getActiveWindow();
113

    
114
        try {
115
            if (e.getID() == KeyEvent.KEY_PRESSED
116
                || !(view instanceof LayoutPanel)
117
                || !(e.getSource() instanceof LayoutControl))
118
                return false;
119
            LayoutPanel layout = (LayoutPanel) view;
120
            if (!layout.getLayoutContext().isEditable())
121
                return false;
122
            IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
123
            int dif = 10;
124
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
125
                switch (e.getKeyCode()) {
126
                case KeyEvent.VK_C:
127
                    copy(layout);
128
                    break;
129
                case KeyEvent.VK_X:
130
                    cut(layout);
131
                    break;
132
                case KeyEvent.VK_V:
133
                    paste(layout);
134
                    break;
135
                case KeyEvent.VK_Z:
136
                    undo(layout);
137
                    break;
138
                case KeyEvent.VK_Y:
139
                    redo(layout);
140
                    break;
141
                }
142
            } else {
143
                switch (e.getKeyCode()) {
144
                case KeyEvent.VK_LEFT:
145
                    difX = difX - dif;
146
                    break;
147

    
148
                case KeyEvent.VK_RIGHT:
149
                    difX = difX + dif;
150
                    break;
151
                case KeyEvent.VK_UP:
152
                    difY = difY - dif;
153
                    break;
154
                case KeyEvent.VK_DOWN:
155
                    difY = difY + dif;
156
                    break;
157
                case KeyEvent.VK_DELETE:
158
                case KeyEvent.VK_BACK_SPACE:
159
                    layout.getLayoutContext().delFFrameSelected();
160
                    layout.getLayoutControl().refresh();
161
                    break;
162
                }
163
                if (e.getKeyCode() == KeyEvent.VK_LEFT
164
                    || e.getKeyCode() == KeyEvent.VK_RIGHT
165
                    || e.getKeyCode() == KeyEvent.VK_UP
166
                    || e.getKeyCode() == KeyEvent.VK_DOWN) {
167
                    if (fframes.length == 0)
168
                        return false;
169
                    layout
170
                        .getLayoutContext()
171
                        .getFrameCommandsRecord()
172
                        .startComplex(
173
                            PluginServices.getText(this, "move_elements"));
174
                    for (int i = 0; i < fframes.length; i++) {
175
                        IFFrame fframeAux = (IFFrame) fframes[i].clone();
176
                        Rectangle2D r =
177
                            getRectMove(fframes[i].getBoundingBox(layout
178
                                .getLayoutControl().getAT()), difX, difY);
179
                        fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(r,
180
                            layout.getLayoutControl().getAT()));
181
                        layout.getLayoutContext().getFrameCommandsRecord()
182
                            .update(fframes[i], fframeAux);
183
                    }
184
                    layout.getLayoutContext().getFrameCommandsRecord()
185
                        .endComplex();
186
                    layout.getLayoutContext().updateFFrames();
187
                    layout.getLayoutControl().refresh();
188
                }
189
                difX = 0;
190
                difY = 0;
191

    
192
            }
193
            return true;
194
        } catch (CloneNotSupportedException e1) {
195
            NotificationManager.addError(e1);
196
            return false;
197
        }
198
    }
199

    
200
    private Rectangle2D getRectMove(Rectangle2D r, int difX, int difY) {
201
        Rectangle2D rectMove = new Rectangle2D.Double();
202
        rectMove.setRect(r.getX() + difX, r.getY() + difY, r.getWidth(),
203
            r.getHeight());
204
        return rectMove;
205
    }
206

    
207
    public static boolean hasSelection() {
208
        return selectFFrames.length > 0;
209
    }
210

    
211
}