Skip to content
Snippets Groups Projects
Commit 69acbf98 authored by Florian Hölzl's avatar Florian Hölzl
Browse files

Added utility class for managing SWT graphics context state more easily.

refs 2589
parent 04476774
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2016 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.utils;
import java.util.Deque;
import java.util.LinkedList;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
/**
* Utility class for handling of {@link GC SWT graphics context} state and data.
* <P>
* Currently, this class only encapsulates the {@link GCStateManager} class, which can be used to
* store and restore attributes of the graphics context.
*
* @author hoelzl
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating YELLOW Hash: A85DD64B7EDBBAD1E1E3E8699FF2B4AD
*/
public final class GCUtils {
/** Creates the state manager for the given graphics context. */
public static GCUtils.GCStateManager createGraphicsContextManager(GC gc) {
return new GCUtils.GCStateManager(gc);
}
/**
* Utility class managing SWT graphics context state by storing and restoring state attributes
* of the underlying graphics context using the provided public methods.
* <P>
* Note that it is the obligation of the user of this class to ensure that methods are called in
* order of the stack principle. Otherwise, {@link ClassCastException}s will occur.
*/
public static final class GCStateManager {
/** The graphics context to be managed. */
private final GC gc;
/** The stack used to store the current gc state attributes. */
private final Deque<Object> state = new LinkedList<Object>();
/** Constructor. */
public GCStateManager(GC gc) {
this.gc = gc;
}
/** Returns the graphics context. */
public GC GC() {
return gc;
}
/** Stores the alpha value. */
public GCStateManager alphaStoreAndSet(int alpha) {
state.push(new Integer(gc.getAlpha()));
gc.setAlpha(alpha);
return this;
}
/** Restores the alpha value. */
public GCStateManager alphaRestore() {
gc.setAlpha((Integer)state.pop());
return this;
}
/** Stores the current foreground color and creates and activates the given color. */
public GCStateManager foregroundStoreAndCreate(RGB rgb) {
state.push(gc.getForeground());
gc.setForeground(new Color(Display.getCurrent(), rgb));
return this;
}
/** Restores the previous foreground color and disposes the current one. */
public GCStateManager foregroundRestoreAndDispose() {
gc.getForeground().dispose();
gc.setForeground((Color)state.pop());
return this;
}
/** Stores the current background color and creates and activates the given color. */
public GCStateManager backgroundStoreAndCreate(RGB rgb) {
state.push(gc.getBackground());
gc.setBackground(new Color(Display.getCurrent(), rgb));
return this;
}
/** Restores the previous background color and disposes the current one. */
public GCStateManager backgroundRestoreAndDispose() {
gc.getBackground().dispose();
gc.setBackground((Color)state.pop());
return this;
}
/** Stores the current font and creates and activates the given font. */
public GCStateManager fontStoreAndCreate(FontData fontData) {
state.push(gc.getFont());
gc.setFont(new Font(Display.getCurrent(), fontData));
return this;
}
/** Restores the previous font and disposes the current one. */
public GCStateManager fontRestoreAndDispose() {
gc.getFont().dispose();
gc.setFont((Font)state.pop());
return this;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment