Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 919801ecea2a28c63ecb9d68401e3ed99cf6b125 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.jface.interfaces;

import org.eclipse.jface.dialogs.IMessageProvider;


/**
 * Interface to be implemented by container managing the validation
 * of contained validatable sub elements.
 */
public interface IValidatingContainer {

	public static class ValidationResult implements IMessageProvider {

		private String message = null;
		private int type = -1;
		private boolean valid = true;

		/**
		 * Set the result from the given message provider.
		 * If the provider is a ValidationResult, the valid state is used too.
		 * If the provider is <code>null</code>, the message is set to <code>null</code> and the type to IMessageProvider.NONE.
		 * The new result is set or not in the same way as setResult(type,message) or setResult(valid,type,message) works.
		 * @param provider The message provider or <code>null</code>.
		 */
		public void setResult(IMessageProvider provider) {
			if (provider instanceof ValidationResult) {
				setResult(provider.getMessage(), provider.getMessageType(), ((ValidationResult)provider).isValid());
			}
			else if (provider != null) {
				setResult(provider.getMessage(), provider.getMessageType());
			}
			else {
				setResult(null, NONE);
			}
		}

		/**
		 * Set the message type and text,
		 * if type > actual type.
		 * @param message The message text.
		 * @param type The message type.
		 */
		public void setResult(String message, int type) {
			if (this.type < type) {
				this.message = message;
				this.type = type;
			}
		}

		/**
		 * Set the validation result, message type and text,
		 * if type > actual type or !valid.
		 * @param message The message text.
		 * @param type The message type.
		 * @param valid The validation result.
		 */
		public void setResult(String message, int type, boolean valid) {
			if (this.type < type || (!valid && this.valid)) {
				this.message = message;
				this.type = type;
				if (this.valid) {
					this.valid = valid;
				}
			}
		}

		/**
		 * Set the message text.
		 * @param message The message text.
		 */
		public void setMessage(String message) {
			this.message = message;
		}

		/**
		 * Set the message type.
		 * @param type The message type.
		 */
		public void setMessageType(int type) {
			this.type = type;
		}

		/**
		 * Set the validation result.
		 * A validation can be valid even when message and message type are set.
		 * @param valid The validation result.
		 */
		public void setValid(boolean valid) {
			this.valid = valid;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessage()
		 */
        @Override
        public String getMessage() {
	        return message;
        }

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType()
		 */
        @Override
        public int getMessageType() {
	        return type;
        }

        /**
         * Return <code>true</code> if the result can be seen as valid.
         */
        public boolean isValid() {
        	return valid;
        }
	}

	/**
	 * Validates the container status.
	 * <p>
	 * If necessary, set the corresponding messages and message types to signal when some sub
	 * elements of the container needs user attention.
	 */
	public void validate();
}

Back to the top