Project

General

Profile

« Previous | Next » 

Revision 64301fda

Added by Katja Luther over 4 years ago

avoid potential widget is disposed exception

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/SelectionArbitrator.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
38 38
	private IEntityElement entityElement;
39 39

  
40 40
	private Set<ISelectionProvider> selectionProviders = new HashSet<ISelectionProvider>();
41
	
41

  
42 42
	/**
43 43
	 * <p>Constructor for SelectionArbitrator.</p>
44 44
	 */
45 45
	public SelectionArbitrator(){}
46
	
46

  
47 47
	/**
48 48
	 * <p>Constructor for SelectionArbitrator.</p>
49 49
	 *
......
52 52
	public SelectionArbitrator(IEntityElement selectableComposite){
53 53
		this.entityElement = selectableComposite;
54 54
	}
55
	
55

  
56 56
	/** {@inheritDoc} */
57
	public void widgetSelected(SelectionEvent e) {
57
	@Override
58
    public void widgetSelected(SelectionEvent e) {
58 59
		Widget widget = e.widget;
59
		
60
		if(widget instanceof Control){
60

  
61
		if(widget instanceof Control && !widget.isDisposed()){
61 62
			Control control = (Control) widget;
62 63
			if(isWidgetPartOfFormElement(control, entityElement)){
63 64
				if(entityElement.getEntity() != null){
......
65 66
					setSelection(selection);
66 67
				}
67 68
			}
68
		}	
69
		}
69 70
	}
70
	
71

  
71 72
	private void setSelection(ISelection selection){
72 73
		for(ISelectionProvider selectionProvider : selectionProviders){
73 74
			selectionProvider.setSelection(selection);
74 75
		}
75 76
	}
76
	
77

  
77 78
	private boolean isWidgetPartOfFormElement(Control widget, ICdmFormElement element){
78 79
		// check if widget is the controls
79 80
		if(widget.equals(element.getLayoutComposite())){
80 81
			return true;
81 82
		}
82
		
83

  
83 84
		// check if widget is controlled by the given element
84 85
		for(Control control : element.getControls()){
85
			if(control.equals(widget)) return true;
86
			if(control.equals(widget)) {
87
                return true;
88
            }
86 89
		}
87
		
90

  
88 91
		// descend into child elements
89 92
		for(ICdmFormElement childElement : element.getElements()){
90 93
			isWidgetPartOfFormElement(widget, childElement);
91 94
		}
92
		
95

  
93 96
		return false;
94 97
	}
95
	
98

  
96 99
	/**
97 100
	 * <p>addSelectionProvider</p>
98 101
	 *
......
101 104
	public void addSelectionProvider(ISelectionProvider selectionProvider){
102 105
		selectionProviders.add(selectionProvider);
103 106
	}
104
	
107

  
105 108
	/**
106 109
	 * <p>removeSelectionProvider</p>
107 110
	 *
......
110 113
	public void removeSelectionProvider(ISelectionProvider selectionProvider){
111 114
		selectionProviders.remove(selectionProvider);
112 115
	}
113
	
116

  
114 117
	/** {@inheritDoc} */
115
	public void widgetDefaultSelected(SelectionEvent e) {}
118
	@Override
119
    public void widgetDefaultSelected(SelectionEvent e) {}
116 120

  
117 121
	/**
118 122
	 * <p>Getter for the field <code>entityElement</code>.</p>
......
133 137
	}
134 138

  
135 139
	/** {@inheritDoc} */
136
	public void selectionChanged(SelectionChangedEvent event) {
140
	@Override
141
    public void selectionChanged(SelectionChangedEvent event) {
137 142
//		if(!(event.getSource() instanceof CdmDetailsViewer)){
138 143
//			return;
139 144
//		}
140
		
145

  
141 146
		try{
142 147
		entityElement.setSelected(false);
143 148
		}catch(SWTException e){
144 149
			MessagingUtils.error(getClass(), "Widgets of element are disposed", e);
145 150
		}
146
		ISelection selection = event.getSelection();		
151
		ISelection selection = event.getSelection();
147 152
		if(selection != null && selection instanceof StructuredSelection){
148 153
			Object selectedElement = ((StructuredSelection) selection).getFirstElement();
149 154
			if(entityElement.getEntity() != null && entityElement.getEntity().equals(selectedElement)){

Also available in: Unified diff