Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / LayoutKeyEvent.java @ 308

History | View | Annotate | Download (8.06 KB)

1 5 jldominguez
/* 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 208 cmartinez
import org.gvsig.app.project.documents.view.toc.DnDJTree;
34 5 jldominguez
import org.gvsig.tools.undo.RedoException;
35
import org.gvsig.tools.undo.UndoException;
36
37
public class LayoutKeyEvent implements KeyEventDispatcher {
38
39
    private static IFFrame[] selectFFrames = new IFFrame[0];
40
    private int difX;
41
    private int difY;
42
43
    public static boolean copy(LayoutPanel layout)
44
        throws CloneNotSupportedException {
45 140 cmartinez
        IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
46 5 jldominguez
        if (fframes.length == 0)
47
            return false;
48
        selectFFrames = new IFFrame[fframes.length];
49
        for (int i = 0; i < fframes.length; i++) {
50
            selectFFrames[i] = (IFFrame) fframes[i].clone();
51
        }
52
        return true;
53
    }
54
55
    public static boolean cut(LayoutPanel layout)
56
        throws CloneNotSupportedException {
57 140 cmartinez
        IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
58 5 jldominguez
        if (fframes.length == 0)
59
            return false;
60
        selectFFrames = new IFFrame[fframes.length];
61
        for (int i = 0; i < fframes.length; i++) {
62
            selectFFrames[i] = (IFFrame) fframes[i].clone();
63
        }
64
        layout.getLayoutContext().delFFrameSelected();
65
        layout.getLayoutContext().notifAllObservers();
66
        return true;
67
    }
68
69
    public static boolean paste(LayoutPanel layout)
70
        throws CloneNotSupportedException {
71
        IFFrame copyFFrame = null;
72
        layout.getLayoutContext().getFrameCommandsRecord()
73
            .startComplex(PluginServices.getText(layout, "paste_elements"));
74
        for (int i = 0; i < selectFFrames.length; i++) {
75
            copyFFrame = (IFFrame) selectFFrames[i].clone();
76
            if (i == 0)
77
                layout.getLayoutContext().addFFrame(copyFFrame, true, true);
78
            else
79
                layout.getLayoutContext().addFFrame(copyFFrame, false, true);
80
        }
81
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
82
        layout.getLayoutContext().notifAllObservers();
83
        return true;
84
    }
85
86
    public static boolean undo(LayoutPanel layout) {
87
        LayoutContext lcontext = layout.getLayoutContext();
88
        try {
89
            lcontext.getFrameCommandsRecord().undo();
90
        } catch (UndoException e) {
91
            e.printStackTrace();
92
        }
93
        layout.getLayoutContext().updateFFrames();
94
        layout.getLayoutContext().notifAllObservers();
95
        PluginServices.getMainFrame().enableControls();
96
        return true;
97
    }
98
99
    public static boolean redo(LayoutPanel layout) {
100
        LayoutContext lcontext = layout.getLayoutContext();
101
        try {
102
            lcontext.getFrameCommandsRecord().redo();
103
        } catch (RedoException e) {
104
            e.printStackTrace();
105
        }
106
        lcontext.updateFFrames();
107
        layout.getLayoutContext().notifAllObservers();
108
        PluginServices.getMainFrame().enableControls();
109
        return true;
110
    }
111
112
    public boolean dispatchKeyEvent(KeyEvent e) {
113
        IWindow view = PluginServices.getMDIManager().getActiveWindow();
114
115
        try {
116
            if (e.getID() == KeyEvent.KEY_PRESSED
117 208 cmartinez
                || !(view instanceof LayoutPanel)
118
                || !( (e.getSource() instanceof LayoutControl)
119
                                || (e.getSource() instanceof DnDJTree)))
120 5 jldominguez
                return false;
121
            LayoutPanel layout = (LayoutPanel) view;
122
            if (!layout.getLayoutContext().isEditable())
123
                return false;
124 140 cmartinez
            IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
125 5 jldominguez
            int dif = 10;
126
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
127
                switch (e.getKeyCode()) {
128
                case KeyEvent.VK_C:
129
                    copy(layout);
130
                    break;
131
                case KeyEvent.VK_X:
132
                    cut(layout);
133
                    break;
134
                case KeyEvent.VK_V:
135
                    paste(layout);
136
                    break;
137
                case KeyEvent.VK_Z:
138
                    undo(layout);
139
                    break;
140
                case KeyEvent.VK_Y:
141
                    redo(layout);
142
                    break;
143
                }
144
            } else {
145
                switch (e.getKeyCode()) {
146
                case KeyEvent.VK_LEFT:
147
                    difX = difX - dif;
148
                    break;
149
150
                case KeyEvent.VK_RIGHT:
151
                    difX = difX + dif;
152
                    break;
153
                case KeyEvent.VK_UP:
154
                    difY = difY - dif;
155
                    break;
156
                case KeyEvent.VK_DOWN:
157
                    difY = difY + dif;
158
                    break;
159
                case KeyEvent.VK_DELETE:
160
                case KeyEvent.VK_BACK_SPACE:
161
                    layout.getLayoutContext().delFFrameSelected();
162
                    layout.getLayoutControl().refresh();
163
                    break;
164
                }
165
                if (e.getKeyCode() == KeyEvent.VK_LEFT
166
                    || e.getKeyCode() == KeyEvent.VK_RIGHT
167
                    || e.getKeyCode() == KeyEvent.VK_UP
168
                    || e.getKeyCode() == KeyEvent.VK_DOWN) {
169
                    if (fframes.length == 0)
170
                        return false;
171
                    layout
172
                        .getLayoutContext()
173
                        .getFrameCommandsRecord()
174
                        .startComplex(
175
                            PluginServices.getText(this, "move_elements"));
176
                    for (int i = 0; i < fframes.length; i++) {
177
                        IFFrame fframeAux = (IFFrame) fframes[i].clone();
178
                        Rectangle2D r =
179
                            getRectMove(fframes[i].getBoundingBox(layout
180
                                .getLayoutControl().getAT()), difX, difY);
181
                        fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(r,
182
                            layout.getLayoutControl().getAT()));
183
                        layout.getLayoutContext().getFrameCommandsRecord()
184
                            .update(fframes[i], fframeAux);
185
                    }
186
                    layout.getLayoutContext().getFrameCommandsRecord()
187
                        .endComplex();
188
                    layout.getLayoutContext().updateFFrames();
189
                    layout.getLayoutControl().refresh();
190
                }
191
                difX = 0;
192
                difY = 0;
193
194
            }
195
            return true;
196
        } catch (CloneNotSupportedException e1) {
197
            NotificationManager.addError(e1);
198
            return false;
199
        }
200
    }
201
202
    private Rectangle2D getRectMove(Rectangle2D r, int difX, int difY) {
203
        Rectangle2D rectMove = new Rectangle2D.Double();
204
        rectMove.setRect(r.getX() + difX, r.getY() + difY, r.getWidth(),
205
            r.getHeight());
206
        return rectMove;
207
    }
208
209
    public static boolean hasSelection() {
210
        return selectFFrames.length > 0;
211
    }
212
213
}