View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.decorators;
17  
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  
24  import org.argeo.slc.SlcException;
25  import org.argeo.slc.SlcNames;
26  import org.argeo.slc.client.ui.ClientUiPlugin;
27  import org.argeo.slc.client.ui.SlcImages;
28  import org.argeo.slc.client.ui.SlcUiConstants;
29  import org.argeo.slc.client.ui.model.ResultFolder;
30  import org.argeo.slc.client.ui.model.ResultParent;
31  import org.argeo.slc.client.ui.model.SingleResultNode;
32  import org.eclipse.jface.resource.ImageDescriptor;
33  import org.eclipse.jface.viewers.DecorationOverlayIcon;
34  import org.eclipse.jface.viewers.IDecoration;
35  import org.eclipse.jface.viewers.ILabelDecorator;
36  import org.eclipse.jface.viewers.LabelProvider;
37  import org.eclipse.swt.graphics.Image;
38  import org.eclipse.ui.ISharedImages;
39  
40  /** Dynamically decorates the results tree. */
41  public class ResultFailedDecorator extends LabelProvider implements
42  		ILabelDecorator {
43  
44  	// FIXME why not use? org.eclipse.jface.viewers.DecoratingLabelProvider
45  
46  	// private final static Log log = LogFactory
47  	// .getLog(ResultFailedDecorator.class);
48  
49  	private final static DateFormat dateFormat = new SimpleDateFormat(
50  			SlcUiConstants.DEFAULT_DISPLAY_DATE_TIME_FORMAT);
51  
52  	// hack for SWT resource leak
53  	// see http://www.eclipse.org/articles/swt-design-2/swt-design-2.html
54  	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=181215
55  	private final Image passedFolder;
56  	private final Image failedFolder;
57  	private final Image failedSingleResult;
58  	private final Image errorSingleResult;
59  	private final Image passedSingleResult;
60  
61  	public ResultFailedDecorator() {
62  		super();
63  		ImageDescriptor failedDesc = ClientUiPlugin.getDefault().getWorkbench()
64  				.getSharedImages()
65  				.getImageDescriptor(ISharedImages.IMG_DEC_FIELD_ERROR);
66  		failedFolder = new DecorationOverlayIcon(SlcImages.FOLDER, failedDesc,
67  				IDecoration.TOP_LEFT).createImage();
68  		passedFolder = new DecorationOverlayIcon(SlcImages.FOLDER,
69  				SlcImages.EXECUTION_PASSED, IDecoration.TOP_LEFT).createImage();
70  		failedSingleResult = new DecorationOverlayIcon(
71  				SlcImages.PROCESS_COMPLETED, failedDesc, IDecoration.TOP_LEFT)
72  				.createImage();
73  		errorSingleResult = new DecorationOverlayIcon(
74  				SlcImages.PROCESS_COMPLETED, SlcImages.EXECUTION_ERROR,
75  				IDecoration.TOP_LEFT).createImage();
76  		passedSingleResult = new DecorationOverlayIcon(
77  				SlcImages.PROCESS_COMPLETED, SlcImages.EXECUTION_PASSED,
78  				IDecoration.TOP_LEFT).createImage();
79  	}
80  
81  	// Method to decorate Image
82  	public Image decorateImage(Image image, Object object) {
83  
84  		// This method returns an annotated image or null if the
85  		// image need not be decorated. Returning a null image
86  		// decorates resource icon with basic decorations provided
87  		// by Eclipse
88  		if (object instanceof ResultParent) {
89  			if (((ResultParent) object).isPassed()) {
90  				if (object instanceof SingleResultNode)
91  					return passedSingleResult;
92  				else if (object instanceof ResultFolder)
93  					return passedFolder;
94  				else
95  					return null;
96  			} else {
97  				// ImageDescriptor desc = ClientUiPlugin.getDefault()
98  				// .getWorkbench().getSharedImages()
99  				// .getImageDescriptor(ISharedImages.IMG_DEC_FIELD_ERROR);
100 				// DecorationOverlayIcon decoratedImage = new
101 				// DecorationOverlayIcon(
102 				// image, desc, IDecoration.TOP_LEFT);
103 				// return decoratedImage.createImage();
104 				if (object instanceof SingleResultNode) {
105 					SingleResultNode srn = (SingleResultNode) object;
106 					boolean isError = false;
107 					try {
108 						isError = srn.getNode()
109 								.getNode(SlcNames.SLC_AGGREGATED_STATUS)
110 								.hasProperty(SlcNames.SLC_ERROR_MESSAGE);
111 					} catch (RepositoryException re) {
112 						// Silent node might not exist
113 					}
114 					if (isError)
115 						return errorSingleResult;
116 					else
117 						return failedSingleResult;
118 
119 				} else
120 					return failedFolder;
121 			}
122 		}
123 		return null;
124 	}
125 
126 	// Method to decorate Text
127 	public String decorateText(String label, Object object) {
128 		if (object instanceof SingleResultNode) {
129 			SingleResultNode srNode = (SingleResultNode) object;
130 			Node node = srNode.getNode();
131 			String decoration = null;
132 			try {
133 				if (node.hasProperty(SlcNames.SLC_COMPLETED))
134 					decoration = dateFormat.format(node
135 							.getProperty(SlcNames.SLC_COMPLETED).getDate()
136 							.getTime());
137 			} catch (RepositoryException re) {
138 				throw new SlcException(
139 						"Unexpected error defining text decoration for result",
140 						re);
141 			}
142 			return label + " [" + decoration + "]";
143 		} else
144 			return null;
145 	}
146 
147 	@Override
148 	public void dispose() {
149 		failedFolder.dispose();
150 		failedSingleResult.dispose();
151 		super.dispose();
152 	}
153 
154 }