Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e92afb45811c4e18b5c5eb1850da616a135c5a3 (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
/*******************************************************************************
 * Copyright (c) 2008, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.ui.internal.platform.generic;

import org.eclipse.jpt.common.ui.internal.jface.AbstractItemExtendedLabelProvider;
import org.eclipse.jpt.common.ui.jface.ItemLabelProvider;
import org.eclipse.jpt.common.utility.internal.ArrayTools;
import org.eclipse.jpt.common.utility.internal.model.value.CompositePropertyValueModel;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyAspectAdapter;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.jpa.core.context.JpaContextNode;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpa.ui.internal.JptUiIcons;
import org.eclipse.jpt.jpa.ui.internal.plugin.JptJpaUiPlugin;
import org.eclipse.swt.graphics.Image;

public class PersistenceUnitItemLabelProvider
	extends AbstractItemExtendedLabelProvider<PersistenceUnit>
{
	protected final Image image;

	public PersistenceUnitItemLabelProvider(PersistenceUnit persistenceUnit, ItemLabelProvider.Manager manager) {
		super(persistenceUnit, manager);
		this.image = this.buildImage();
	}

	@Override
	public Image getImage() {
		return this.image;
	}

	protected Image buildImage() {
		return JptJpaUiPlugin.instance().getImage(JptUiIcons.PERSISTENCE_UNIT);
	}
	
	@Override
	protected PropertyValueModel<String> buildTextModel() {
		return new TextModel(this.item);
	}

	protected static class TextModel
		extends PropertyAspectAdapter<PersistenceUnit, String>
	{
		public TextModel(PersistenceUnit subject) {
			super(PersistenceUnit.NAME_PROPERTY, subject);
		}
		@Override
		protected String buildValue_() {
			return this.subject.getName();
		}
	}
	
	@Override
	protected PropertyValueModel<String> buildDescriptionModel() {
		return new DescriptionModel(this.item);
	}

	protected static class DescriptionModel
		extends TextModel
	{
		public DescriptionModel(PersistenceUnit subject) {
			super(subject);
		}
		@Override
		protected String buildValue_() {
			StringBuilder sb = new StringBuilder();
			sb.append(super.buildValue_());
			sb.append(" - ");  //$NON-NLS-1$
			sb.append(this.subject.getResource().getFullPath().makeRelative());
			return sb.toString();
		}
	}
	

	// ********** component description model **********

	@SuppressWarnings("unchecked")
	public static PropertyValueModel<String> buildQuotedComponentDescriptionModel(JpaContextNode node, PropertyValueModel<String> nodeTextModel) {
		return buildComponentDescriptionModel(node, true, nodeTextModel);
	}
		
	public static PropertyValueModel<String> buildNonQuotedComponentDescriptionModel(JpaContextNode node, PropertyValueModel<String>... nodeTextModels) {
		return buildComponentDescriptionModel(node, false, nodeTextModels);
	}
		
	protected static PropertyValueModel<String> buildComponentDescriptionModel(JpaContextNode node, boolean quote, PropertyValueModel<String>... nodeTextModels) {
		return new ComponentDescriptionModel(
					nodeTextModels,
					new TextModel(node.getPersistenceUnit()),
					node.getResource().getFullPath().makeRelative().toString(),
					quote
				);
	}
		
	public static class ComponentDescriptionModel
		extends CompositePropertyValueModel<String, Object>
	{
		protected final PropertyValueModel<String>[] nodeTextModels;
		protected final PropertyValueModel<String> persistenceUnitNameModel;
		protected final String path;
		protected final boolean quote;

		@SuppressWarnings("unchecked")
		ComponentDescriptionModel(
				PropertyValueModel<String> nodeTextModel,
				PropertyValueModel<String> persistenceUnitNameModel,
				String path,
				boolean quote
		) {
			this(
				new PropertyValueModel[] {nodeTextModel},
				persistenceUnitNameModel,
				path,
				quote
			);
		}

		ComponentDescriptionModel(
				PropertyValueModel<String>[] nodeTextModels,
				PropertyValueModel<String> persistenceUnitNameModel,
				String path,
				boolean quote
		) {
			super(ArrayTools.add(nodeTextModels, persistenceUnitNameModel));
			if (nodeTextModels.length < 1) {
				throw new IllegalArgumentException();
			}
			this.nodeTextModels = nodeTextModels;
			this.persistenceUnitNameModel = persistenceUnitNameModel;
			this.path = path;
			this.quote = quote;
		}

		@Override
		protected String buildValue() {
			StringBuilder sb = new StringBuilder();
			sb.append(this.persistenceUnitNameModel.getValue());
			sb.append('/');
			if (this.quote) {
				sb.append('\"');
			}
			sb.append(this.nodeTextModels[0].getValue());
			for (int i = 1; i < this.nodeTextModels.length; i++) {
				sb.append('/');
				sb.append(this.nodeTextModels[i].getValue());
			}
			if (this.quote) {
				sb.append('\"');
			}
			sb.append(" - "); //$NON-NLS-1$
			sb.append(this.path);
			return sb.toString();
		}
	}
}

Back to the top