summaryrefslogtreecommitdiff
path: root/plugins/ui-designer-core/src/com/intellij/designer/designSurface/tools/TargetingTool.java
blob: 900fe2703ecb7ed7a5f46eec565fbcfeef6f3825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.designer.designSurface.tools;

import com.intellij.designer.designSurface.ComponentTargetFilter;
import com.intellij.designer.designSurface.EditOperation;
import com.intellij.designer.designSurface.EditableArea;
import com.intellij.designer.designSurface.OperationContext;
import com.intellij.designer.model.RadComponent;
import com.intellij.designer.model.RadLayout;
import org.jetbrains.annotations.Nullable;

import java.awt.event.KeyEvent;
import java.util.Collections;

/**
 * @author Alexander Lobas
 */
public abstract class TargetingTool extends InputTool {
  protected OperationContext myContext = new OperationContext();
  protected EditOperation myTargetOperation;
  protected RadComponent myTarget;
  private boolean myShowFeedback;

  @Override
  public void deactivate() {
    eraseFeedback();
    myContext = new OperationContext();
    myTargetOperation = null;
    myTarget = null;
    super.deactivate();
  }

  protected void handleInvalidInput() {
    eraseFeedback();
    setExecuteEnabled(false);
  }

  @Override
  protected void handleDragStarted() {
    if (myState == STATE_DRAG) {
      myState = STATE_DRAG_IN_PROGRESS;
    }
  }

  @Override
  protected void handleAreaExited() {
    setTarget(null, null);
    setExecuteEnabled(false);
  }

  protected void showFeedback() {
    if (myTargetOperation != null) {
      myTargetOperation.showFeedback();
    }
    myShowFeedback = true;
  }

  protected void eraseFeedback() {
    if (myShowFeedback) {
      myShowFeedback = false;
      if (myTargetOperation != null) {
        myTargetOperation.eraseFeedback();
      }
    }
  }

  protected void executeCommand() {
    if (myExecuteEnabled) {
      myToolProvider.execute(Collections.singletonList(myTargetOperation), myContext.getMessage());
    }
  }

  protected void updateCommand() {
    setExecuteEnabled(myTargetOperation != null && myTargetOperation.canExecute());
  }

  protected void updateContext() {
    myContext.setArea(myArea);
    myContext.setInputEvent(myInputEvent);
    myContext.setModifiers(myInputEvent.getModifiers());
  }

  protected void setTarget(@Nullable RadComponent target, @Nullable ContainerTargetFilter filter) {
    if (target != myTarget) {
      if (myTargetOperation != null) {
        eraseFeedback();
      }

      myTarget = target;
      myTargetOperation = filter == null ? null : filter.getOperation();
    }
  }

  @Override
  public void keyPressed(KeyEvent event, EditableArea area) throws Exception {
    boolean changedModifiers = event.getModifiers() != myModifiers;
    super.keyPressed(event, area);

    if (changedModifiers) {
      handleKeyEvent();
    }
  }

  @Override
  public void keyReleased(KeyEvent event, EditableArea area) throws Exception {
    boolean changedModifiers = event.getModifiers() != myModifiers;
    super.keyReleased(event, area);

    if (changedModifiers) {
      handleKeyEvent();
    }
  }

  private void handleKeyEvent() {
    if (myContext != null) {
      updateContext();
      showFeedback();
      updateCommand();
    }
  }

  protected class ContainerTargetFilter implements ComponentTargetFilter {
    private EditOperation myOperation;

    public EditOperation getOperation() {
      return myOperation;
    }

    @Override
    public boolean preFilter(RadComponent component) {
      return true;
    }

    @Override
    public boolean resultFilter(RadComponent target) {
      updateContext(target);

      if (myTarget == target && myOperation != null) {
        return true;
      }

      RadLayout layout = target.getLayout();
      if (layout != null) {
        myContext.setContainer(target);
        myOperation = layout.processChildOperation(myContext);
        myContext.setContainer(null);
      }

      return myOperation != null;
    }

    protected void updateContext(RadComponent target) {
    }
  }
}