Skip to main content
summaryrefslogtreecommitdiffstats
blob: 587928c9865ba05efcc018eaa68db2cef037cb5d (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 SAP AG and others.
 * 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:
 * Kaloyan Raev, kaloyan.raev@sap.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.web.operations;

import static org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties.INTERFACES;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_HTTP_SESSION_ACTIVATION_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_HTTP_SESSION_ATTRIBUTE_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_HTTP_SESSION_BINDING_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_HTTP_SESSION_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_SERVLET_CONTEXT_ATTRIBUTE_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_SERVLET_CONTEXT_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_SERVLET_REQUEST_ATTRIBUTE_LISTENER;
import static org.eclipse.jst.j2ee.web.IServletConstants.QUALIFIED_SERVLET_REQUEST_LISTENER;

import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jst.j2ee.internal.common.operations.NewJavaClassDataModelProvider;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation;
import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin;

/**
 * The NewListenerClassDataModelProvider is a subclass of
 * NewWebClassDataModelProvider and follows the IDataModel Operation and Wizard
 * frameworks.
 * 
 * @see org.eclipse.wst.common.frameworks.datamodel.IDataModelProvider
 * @see org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation
 * 
 * This data model provider is a subclass of the NewWebClassDataModelProvider,
 * which stores base properties necessary in the creation of a default java
 * class.
 * @see org.eclipse.jst.j2ee.internal.common.operations.NewWebClassDataModelProvider
 * 
 * The NewListenerClassDataModelProvider provides more specific properties for
 * java class creation that are required in creating a listener java class. The
 * data model provider is used to store these values for the
 * NewListenerClassOperation.
 * @see org.eclipse.jst.j2ee.internal.web.operations.INewListenerClassDataModelProperties
 *      That operation will create the listener java class based on the settings
 *      defined here in the data model.
 * @see org.eclipse.jst.j2ee.internal.web.operations.NewListenerClassOperation
 * 
 * This data model properties implements the IAnnotationsDataModel to get the
 * USE_ANNOTATIONS property for determining whether or not to generate an
 * annotated java class.
 * @see org.eclipse.jst.j2ee.application.internal.operations.IAnnotationsDataModel
 * 
 * Clients can subclass this data model provider to cache and provide their own
 * specific attributes. They should also provide their own validation methods,
 * properties interface, and default values for the properties they add.
 * 
 * The use of this class is EXPERIMENTAL and is subject to substantial changes.
 */
public class NewListenerClassDataModelProvider extends
		NewWebClassDataModelProvider {

	public static final String[] LISTENER_INTERFACES = {
		QUALIFIED_SERVLET_CONTEXT_LISTENER,	
		QUALIFIED_SERVLET_CONTEXT_ATTRIBUTE_LISTENER,
		QUALIFIED_HTTP_SESSION_LISTENER,
		QUALIFIED_HTTP_SESSION_ATTRIBUTE_LISTENER,
		QUALIFIED_HTTP_SESSION_ACTIVATION_LISTENER,
		QUALIFIED_HTTP_SESSION_BINDING_LISTENER,
		QUALIFIED_SERVLET_REQUEST_LISTENER,
		QUALIFIED_SERVLET_REQUEST_ATTRIBUTE_LISTENER
	};

	/**
	 * Subclasses may extend this method to provide their own default operation
	 * for this data model provider. This implementation uses the
	 * AddListenerOperation to drive the listener creation. It will not return
	 * null.
	 * 
	 * @see IDataModel#getDefaultOperation()
	 * 
	 * @return IDataModelOperation AddListenerOperation
	 */
	@Override
	public IDataModelOperation getDefaultOperation() {
		return new AddListenerOperation(getDataModel());
	}

	/**
	 * Subclasses may extend this method to provide their own validation on any
	 * of the valid data model properties in the hierarchy. It does not accept a
	 * null parameter. This method will not return null.
	 * 
	 * @see NewJavaClassDataModelProvider#validate(String)
	 * 
	 * @param propertyName
	 * @return IStatus is property value valid?
	 */
	@Override
	public IStatus validate(String propertyName) {
		if (propertyName.equals(INTERFACES)) {
			return validateListeners();
		}
		
		// Otherwise defer to super to validate the property
		return super.validate(propertyName);
	}
	

	/**
	 * Checks if at least one of the application lifecycle listeners is
	 * selected.
	 */
	private IStatus validateListeners() {
		boolean atLeastOneSelected = false;
		Object value = model.getProperty(INTERFACES);
		if (value != null && (value instanceof List)) {
			List interfaces = (List) value;
			for (String iface : LISTENER_INTERFACES) {
				if (interfaces.contains(iface)) { 
					atLeastOneSelected = true;
					break;
				}
			}
		}
		
		if (atLeastOneSelected) {
			return WTPCommonPlugin.OK_STATUS;
		}
		String msg = WebMessages.ERR_NO_LISTENER_SELECTED;
		return WTPCommonPlugin.createErrorStatus(msg);
	}

}

Back to the top