Skip to main content
summaryrefslogtreecommitdiffstats
blob: cbe702a5972b0ca4209d63ed9e088f34907cf5df (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 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.db.internal;

import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObjectListener;
import org.eclipse.datatools.connectivity.sqm.core.rte.RefreshManager;
import org.eclipse.jpt.utility.internal.StringTools;

/**
 *  DTP Catalog Object Wrapper base class
 */
abstract class DTPWrapper
	implements ConnectionProfileHolder
{
	// we need a way to get to the connection profile
	private final ConnectionProfileHolder connectionProfileHolder;

	// listen for the "catalog object" being refreshed
	private final ICatalogObjectListener catalogObjectListener;

	// we only listen to "live" connections
	private final boolean connectionIsLive;

	// listen for this to refresh
	final ICatalogObject catalogObject;


	DTPWrapper(ConnectionProfileHolder connectionProfileHolder, Object dtpObject) {
		super();
		this.connectionProfileHolder = connectionProfileHolder;
		this.connectionIsLive = this.getConnectionProfile().isConnected();
		if (this.connectionIsLive) {
			this.catalogObject = (ICatalogObject) dtpObject;
			this.catalogObjectListener = this.buildCatalogObjectListener();
			RefreshManager.getInstance().AddListener(this.catalogObject, this.catalogObjectListener);
		} else {
			this.catalogObject = null;
			this.catalogObjectListener = null;
		}
	}

	private ICatalogObjectListener buildCatalogObjectListener() {
		return new ICatalogObjectListener() {
			public void notifyChanged(ICatalogObject dmElement, int eventType) {
				if (dmElement == DTPWrapper.this.catalogObject) {
					DTPWrapper.this.catalogObjectChanged(eventType);
				}
			}
		};
	}

	// typically, notify the connection profile something has changed
	abstract void catalogObjectChanged(int eventType);

	public DTPConnectionProfileWrapper getConnectionProfile() {
		return this.connectionProfileHolder.getConnectionProfile();
	}

	void dispose() {
		if (this.connectionIsLive) {
	        RefreshManager.getInstance().removeListener(this.catalogObject, this.catalogObjectListener);
		}
	}

	// all the subclasses can implement this method
	abstract String getName();

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.getName());
	}

}

Back to the top