Passed
Push — master ( cc2ba8...b3c729 )
by Christoph
05:03
created

update(Profile)   C

Complexity

Conditions 10

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 28
dl 0
loc 37
ccs 0
cts 24
cp 0
crap 110
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like net.sourceforge.fullsync.ui.NiceListViewItem.update(Profile) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * This program is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU General Public License
4
 * as published by the Free Software Foundation; either version 2
5
 * of the License, or (at your option) any later version.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
15
 * Boston, MA 02110-1301, USA.
16
 *
17
 * For information about the authors of this project Have a look
18
 * at the AUTHORS file in the root of this project.
19
 */
20
package net.sourceforge.fullsync.ui;
21
22
import javax.inject.Inject;
23
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.KeyEvent;
26
import org.eclipse.swt.graphics.Color;
27
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.graphics.Point;
29
import org.eclipse.swt.layout.GridData;
30
import org.eclipse.swt.layout.GridLayout;
31
import org.eclipse.swt.widgets.Composite;
32
import org.eclipse.swt.widgets.Control;
33
import org.eclipse.swt.widgets.Display;
34
import org.eclipse.swt.widgets.Event;
35
import org.eclipse.swt.widgets.Label;
36
import org.eclipse.swt.widgets.Listener;
37
import org.eclipse.swt.widgets.ToolBar;
38
import org.eclipse.swt.widgets.ToolItem;
39
40
import com.google.inject.assistedinject.Assisted;
41
42
import net.sourceforge.fullsync.ConnectionDescription;
43
import net.sourceforge.fullsync.Profile;
44
45
class NiceListViewItem extends Composite implements Listener {
46
	private final NiceListView list;
47
	private final ImageRepository imageRepository;
48
	private Label labelIcon;
49
	private Label labelCaption;
50
	private Label labelStatus;
51
	private Composite compositeContent;
52
	private Label labelSource;
53
	private Label labelDestination;
54
	private Label labelLastUpdate;
55
	private Label labelNextUpdate;
56
	private ProfileListControlHandler handler;
57
	private Profile profile;
58
	private boolean mouseOver;
59
	private boolean selected;
60
61
	@Inject
62
	NiceListViewItem(ImageRepository imageRepository, FontRepository fontRepository, @Assisted NiceListView parent,
63
		@Assisted ProfileListControlHandler handler) {
64
		super(parent, SWT.NULL);
65
		this.imageRepository = imageRepository;
66
		this.list = parent;
67
		this.handler = handler;
68
		this.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
69
70
		GridLayout thisLayout = new GridLayout(3, false);
71
		this.addListener(SWT.MouseEnter, this);
72
		this.addListener(SWT.MouseExit, this);
73
		this.addListener(SWT.MouseUp, this);
74
		this.addListener(SWT.MouseDown, this);
75
		this.addListener(SWT.MouseDoubleClick, this);
76
		this.addListener(SWT.KeyDown, this);
77
		this.addListener(SWT.FocusIn, this);
78
		this.addListener(SWT.FocusOut, this);
79
		this.setLayout(thisLayout);
80
		thisLayout.marginHeight = 3;
81
		thisLayout.marginWidth = 3;
82
83
		// icon
84
		labelIcon = new Label(this, SWT.TRANSPARENT);
85
		labelIcon.setSize(16, 16);
86
		GridData labelIconLData = new GridData(GridData.CENTER, GridData.BEGINNING, false, true);
87
		labelIconLData.widthHint = 16;
88
		labelIconLData.heightHint = 16;
89
		labelIcon.setLayoutData(labelIconLData);
90
		labelIcon.addListener(SWT.MouseEnter, this);
91
		labelIcon.addListener(SWT.MouseExit, this);
92
		labelIcon.addListener(SWT.MouseUp, this);
93
		labelIcon.addListener(SWT.MouseDown, this);
94
		labelIcon.addListener(SWT.MouseDoubleClick, this);
95
96
		// profile name
97
		labelCaption = new Label(this, SWT.TRANSPARENT);
98
		labelCaption.setFont(fontRepository.getFont("Tahoma", 9, 1)); //$NON-NLS-1$
99
		GridData labelCaptionLData = new GridData();
100
		labelCaptionLData.widthHint = -1;
101
		labelCaption.setLayoutData(labelCaptionLData);
102
		labelCaption.addListener(SWT.MouseEnter, this);
103
		labelCaption.addListener(SWT.MouseExit, this);
104
		labelCaption.addListener(SWT.MouseUp, this);
105
		labelCaption.addListener(SWT.MouseDown, this);
106
		labelCaption.addListener(SWT.MouseDoubleClick, this);
107
108
		labelStatus = new Label(this, SWT.TRANSPARENT);
109
		GridData labelStatusLData = new GridData(GridData.FILL, GridData.CENTER, true, false);
110
		labelStatusLData.horizontalIndent = 10;
111
		labelStatus.setLayoutData(labelStatusLData);
112
		labelStatus.addListener(SWT.MouseEnter, this);
113
		labelStatus.addListener(SWT.MouseExit, this);
114
		labelStatus.addListener(SWT.MouseUp, this);
115
		labelStatus.addListener(SWT.MouseDown, this);
116
		labelStatus.addListener(SWT.MouseDoubleClick, this);
117
118
		this.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
119
		this.setForeground(parent.getForeground());
120
		this.layout();
121
		this.compositeContent = new Composite(this, SWT.NULL);
122
		createContentComposite(compositeContent);
123
	}
124
125
	private void createContentComposite(Composite parent) {
126
		parent.setBackgroundMode(SWT.INHERIT_FORCE);
127
128
		GridLayout layout = new GridLayout(2, false);
129
		layout.marginHeight = 1;
130
		layout.marginWidth = 1;
131
		layout.verticalSpacing = 2;
132
		layout.horizontalSpacing = 2;
133
		parent.setLayout(layout);
134
135
		Composite cSourceDestination = new Composite(parent, SWT.FILL);
136
		GridLayout sourceDestinationLayout = new GridLayout(2, false);
137
		sourceDestinationLayout.marginHeight = 0;
138
		sourceDestinationLayout.marginWidth = 0;
139
		cSourceDestination.setLayout(sourceDestinationLayout);
140
		cSourceDestination.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
141
		cSourceDestination.setEnabled(false); // passes any events up to the parent
142
143
		// source label
144
		new Label(cSourceDestination, SWT.NULL).setText(Messages.getString("NiceListViewProfileListComposite.Source")); //$NON-NLS-1$
145
		labelSource = new Label(cSourceDestination, SWT.NULL);
146
		labelSource.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
147
148
		// destination label
149
		new Label(cSourceDestination, SWT.NULL).setText(Messages.getString("NiceListViewProfileListComposite.Destination")); //$NON-NLS-1$
150
		labelDestination = new Label(cSourceDestination, SWT.NULL);
151
		labelDestination.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
152
153
		// last / next update
154
		Composite cUpdate = new Composite(parent, SWT.FILL);
155
		GridLayout updateLayout = new GridLayout(2, false);
156
		updateLayout.marginHeight = 0;
157
		updateLayout.marginWidth = 0;
158
		cUpdate.setLayout(updateLayout);
159
		cUpdate.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
160
		cUpdate.setEnabled(false); // passes any events up to the parent
161
162
		// last update
163
		new Label(cUpdate, SWT.NULL).setText(Messages.getString("NiceListViewProfileListComposite.LastUpdate")); //$NON-NLS-1$
164
		labelLastUpdate = new Label(cUpdate, SWT.NULL);
165
		labelLastUpdate.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
166
167
		// next update
168
		new Label(cUpdate, SWT.NULL).setText(Messages.getString("NiceListViewProfileListComposite.NextUpdate")); //$NON-NLS-1$
169
		labelNextUpdate = new Label(cUpdate, SWT.NULL);
170
		labelNextUpdate.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
171
172
		// buttons
173
		ToolBar toolbar = new ToolBar(parent, SWT.FLAT);
174
		toolbar.setLayoutData(new GridData(GridData.END, GridData.CENTER, true, false, 1, 2));
175
176
		ToolItem runProfile = new ToolItem(toolbar, SWT.PUSH);
177
		runProfile.setImage(imageRepository.getImage("Profile_Run.png")); //$NON-NLS-1$
178
		runProfile.addListener(SWT.Selection, e -> handler.runProfile(getProfile(), true));
179
180
		ToolItem runProfileNonInteractive = new ToolItem(toolbar, SWT.PUSH);
181
		runProfileNonInteractive.setImage(imageRepository.getImage("Profile_Run_Non_Inter.png")); //$NON-NLS-1$
182
		runProfileNonInteractive.addListener(SWT.Selection, e -> handler.runProfile(getProfile(), false));
183
184
		ToolItem editProfile = new ToolItem(toolbar, SWT.PUSH);
185
		editProfile.setImage(imageRepository.getImage("Profile_Edit.png")); //$NON-NLS-1$
186
		editProfile.addListener(SWT.Selection, e -> handler.editProfile(getProfile()));
187
188
		ToolItem deleteProfile = new ToolItem(toolbar, SWT.PUSH);
189
		deleteProfile.setImage(imageRepository.getImage("Profile_Delete.png")); //$NON-NLS-1$
190
		deleteProfile.addListener(SWT.Selection, e -> handler.deleteProfile(getProfile()));
191
		GridData compositeContentLData = new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1);
192
		compositeContentLData.horizontalIndent = labelIcon.getBounds().width + 4;
193
		compositeContentLData.heightHint = 0;
194
		compositeContentLData.widthHint = 0;
195
		parent.setLayoutData(compositeContentLData);
196
		parent.setVisible(false);
197
		parent.addListener(SWT.MouseDoubleClick, this);
198
		parent.addListener(SWT.MouseUp, this);
199
		parent.addListener(SWT.MouseDown, this);
200
		Control[] children = parent.getChildren();
201
		for (Control element : children) {
202
			element.addListener(SWT.MouseDoubleClick, this);
203
			element.addListener(SWT.MouseUp, this);
204
			element.addListener(SWT.MouseDown, this);
205
		}
206
	}
207
208
	@Override
209
	public void handleEvent(final Event event) {
210
		switch (event.type) {
211
			case SWT.MouseEnter:
212
				mouseOver = true;
213
				updateBackground();
214
				break;
215
			case SWT.MouseExit:
216
				mouseOver = false;
217
				updateBackground();
218
				break;
219
			case SWT.MouseDown:
220
				list.setSelected(NiceListViewItem.this);
221
				break;
222
			case SWT.MouseUp:
223
				if (event.button == 3) {
224
					getMenu().setVisible(true);
225
				}
226
				break;
227
			case SWT.MouseDoubleClick:
228
				handler.editProfile(profile);
229
				break;
230
			case SWT.KeyDown:
231
				list.keyPressed(new KeyEvent(event));
232
				break;
233
			default:
234
				break;
235
		}
236
	}
237
238
	@Override
239
	public void setBackground(Color color) {
240
		super.setBackground(color);
241
242
		for (Control element : this.getChildren()) {
243
			element.setBackground(color);
244
		}
245
	}
246
247
	@Override
248
	public void setForeground(Color color) {
249
		super.setForeground(color);
250
251
		for (Control element : this.getChildren()) {
252
			element.setForeground(color);
253
		}
254
	}
255
256
	private void updateBackground() {
257
		Display display = getDisplay();
258
		if (selected) {
259
			setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
260
			setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
261
		}
262
		else {
263
			if (mouseOver) {
264
				setBackground(display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
265
			}
266
			else {
267
				setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
268
			}
269
			setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
270
		}
271
	}
272
273
	public void setImage(Image image) {
274
		labelIcon.setImage(image);
275
	}
276
277
	public void setText(String text) {
278
		labelCaption.setText(text);
279
		labelCaption.pack();
280
		layout();
281
	}
282
283
	public void setStatusText(String status) {
284
		if ((null == status) || status.isEmpty()) {
285
			labelStatus.setText(""); //$NON-NLS-1$
286
		}
287
		else {
288
			labelStatus.setText(String.format("(%s)", status)); //$NON-NLS-1$
289
		}
290
		labelStatus.pack();
291
	}
292
293
	public void setSelected(boolean selected) {
294
		if (selected) {
295
			forceFocus();
296
			compositeContent.setVisible(true);
297
			Point size = compositeContent.computeSize(SWT.DEFAULT, SWT.DEFAULT);
298
			((GridData) compositeContent.getLayoutData()).widthHint = size.x;
299
			((GridData) compositeContent.getLayoutData()).heightHint = size.y;
300
		}
301
		else {
302
			compositeContent.setVisible(false);
303
			((GridData) compositeContent.getLayoutData()).widthHint = 0;
304
			((GridData) compositeContent.getLayoutData()).heightHint = 0;
305
		}
306
		this.selected = selected;
307
		updateBackground();
308
		this.layout();
309
	}
310
311
	public Profile getProfile() {
312
		return profile;
313
	}
314
315
	void update(Profile profile) {
316
		this.profile = profile;
317
		boolean isError = profile.getLastErrorLevel() > 0;
318
		boolean isScheduled = profile.isSchedulingEnabled() && (null != profile.getSchedule());
319
		if (isScheduled) {
320
			setImage(isError
321
				? imageRepository.getImage("Profile_Default_Error_Scheduled.png") //$NON-NLS-1$
322
				: imageRepository.getImage("Profile_Default_Scheduled.png")); //$NON-NLS-1$
323
		}
324
		else {
325
			setImage(isError ? imageRepository.getImage("Profile_Default_Error.png") : imageRepository.getImage("Profile_Default.png")); //$NON-NLS-1$ //$NON-NLS-2$
326
		}
327
328
		setText(profile.getName());
329
330
		if (isError) {
331
			setStatusText(profile.getLastErrorString());
332
		}
333
		else {
334
			String desc = profile.getDescription();
335
			if ((null != desc) && !desc.isEmpty()) {
336
				setStatusText(desc);
337
			}
338
			else if (isScheduled) {
339
				setStatusText(profile.getNextUpdateText());
340
			}
341
			else {
342
				setStatusText(""); //$NON-NLS-1$
343
			}
344
		}
345
		ConnectionDescription src = profile.getSource();
346
		labelSource.setText(null != src ? src.getDisplayPath() : ""); //$NON-NLS-1$
347
		ConnectionDescription dst = profile.getDestination();
348
		labelDestination.setText(null != dst ? dst.getDisplayPath() : ""); //$NON-NLS-1$
349
		labelLastUpdate.setText(profile.getLastUpdateText());
350
		labelNextUpdate.setText(profile.getNextUpdateText());
351
		compositeContent.layout();
352
	}
353
}
354