Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 2eaf4bbf19cb443b02a2dbd0e3de076ada9d96fd (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.asd.design.editparts;

import java.util.Iterator;
import java.util.List;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.wsdl.ui.internal.actions.OpenInNewEditor;
import org.eclipse.wst.wsdl.ui.internal.asd.design.editparts.model.IActionProvider;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IASDObject;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IASDObjectListener;
import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.KeyBoardAccessibilityEditPolicy;

public abstract class BaseEditPart extends AbstractGraphicalEditPart implements IActionProvider, IASDObjectListener, IFeedbackHandler
{
  protected static final String[] EMPTY_ACTION_ARRAY = {};
  
  public String[] getActions(Object object)
  {
    Object model = getModel();
    if (model instanceof IActionProvider)
    {
      return ((IActionProvider)model).getActions(object);
    }  
    return EMPTY_ACTION_ARRAY;
  }
  
  protected void addActionsToList(List list, String[] actions)
  {
    for (int i = 0; i < actions.length; i++)
    {
      list.add(actions[i]);
    }  
  }
  
  public void activate()
  {
    super.activate();
    Object model = getModel();    
    if (model instanceof IASDObject)
    {
      IASDObject object = (IASDObject)model;
      object.registerListener(this);
    }      
  }
  
  public void deactivate()
  {
    Object model = getModel();
    if (model instanceof IASDObject)
    {
      IASDObject object = (IASDObject)model;
      object.unregisterListener(this);
    }   
    super.deactivate();
  }  
  
  public void propertyChanged(Object object, String property)
  {
    //System.out.println("propertyChanged " + this.getClass().getName());
    refresh();
  }
  
  public void refreshConnections() {
	  Iterator kids = getChildren().iterator();
	  while (kids.hasNext()) {
		  Object item = kids.next();
		  if (item instanceof BaseEditPart) {
			  ((BaseEditPart) item).refreshConnections();
		  }
	  }
  }
  
  public void addFeedback() {
	  
  }
  public void removeFeedback() {
	  
  }
  
  protected boolean hitTest(Rectangle rectangle, Point location) {
	  return rectangle.contains(location);
  }
  
  protected boolean hitTest(Label target, Point location) {
	  Rectangle origB = target.getTextBounds().getCopy();
	  Rectangle transB = target.getTextBounds().getCopy();

	  target.translateToAbsolute(transB);

	  int newX = origB.x + Math.abs(transB.x - origB.x);
	  int newY = origB.y + Math.abs(transB.y - origB.y);	  
	  Rectangle finalB = new Rectangle(newX, newY, origB.width, origB.height);

	  return finalB.contains(location);
  }
  
  protected boolean hitTestFigure(Figure target, Point location) {
    Rectangle origB = target.getBounds().getCopy();
    Rectangle transB = target.getBounds().getCopy();

    target.translateToAbsolute(transB);

    int newX = origB.x + Math.abs(transB.x - origB.x);
    int newY = origB.y + Math.abs(transB.y - origB.y);    
    Rectangle finalB = new Rectangle(newX, newY, origB.width, origB.height);

    return finalB.contains(location);
  }

  public boolean isReadOnly() {
	  Object model = getModel();
	  if (model instanceof IASDObject) {
		  return ((IASDObject) model).isReadOnly();
	  }
	  
	  return false;
  }
  
  protected void doOpenNewEditor()
  {
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor();
    ActionRegistry registry = (ActionRegistry) editorPart.getAdapter(ActionRegistry.class);
    if (registry != null)
    {
      IAction action = registry.getAction(OpenInNewEditor.ID);
      action.run();
    }
  }
 
  protected void createEditPolicies()
  {      
    KeyBoardAccessibilityEditPolicy navigationEditPolicy = new KeyBoardAccessibilityEditPolicy()
    {           
      public EditPart getRelativeEditPart(EditPart editPart, int direction)
      {          
        return BaseEditPart.this.getRelativeEditPart(direction);            
      }      
    };
    installEditPolicy(KeyBoardAccessibilityEditPolicy.KEY, navigationEditPolicy);    
  }
  
  public EditPart getRelativeEditPart(int direction)
  {
    return EditPartNavigationHandlerUtil.getRelativeEditPart(this, direction);
  }
}

Back to the top