📄 ngshell.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger 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.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.gui;
import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import com.novocode.naf.app.*;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.SizeMeasure;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.gui.image.*;
import com.novocode.naf.model.*;
import com.novocode.naf.resource.*;
/**
* A window.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Nov 27, 2003
*/
public final class NGShell extends NGWindow
{
private static final SizeMeasure DEFAULT_SIZE = new SizeMeasure(SWT.DEFAULT);
private String imageResource;
private SizeMeasure width = DEFAULT_SIZE, height = DEFAULT_SIZE;
private SizeMeasure minWidth = DEFAULT_SIZE, minHeight = DEFAULT_SIZE;
private String[] imageResources = new String[0];
private Trim[] trim = new Trim[] { Trim.CLOSE, Trim.TITLE, Trim.MIN };
private boolean resize = true;
private Position position = Position.DEFAULT;
public enum Position
{
DEFAULT, CENTER_PARENT, CENTER_PARENT_CLIENT_AREA, CENTER_DISPLAY, CENTER_DISPLAY_CLIENT_AREA, FULL_DISPLAY
};
public enum Trim
{
BORDER (SWT.BORDER), CLOSE (SWT.CLOSE), MIN (SWT.MIN), BORDERLESS (SWT.NO_TRIM),
TITLE (SWT.TITLE), SHELL (SWT.CLOSE|SWT.TITLE|SWT.MIN), DIALOG (SWT.DIALOG_TRIM),
// HSCROLL (SWT.H_SCROLL), VSCROLL (SWT.V_SCROLL),
TOP (SWT.ON_TOP), TOOL (SWT.TOOL);
public final int style;
Trim(int style) { this.style = style; }
public static int style(Trim[] a)
{
if(a == null) return 0;
int res = 0;
for(Trim t : a) if(t != null) res |= t.style;
return res;
}
}
public NGShell()
{
modality = Modality.MODELESS;
}
@XMLProperty
public void setResize(boolean b) { resize = b; }
public boolean getResize() { return resize; }
@XMLProperty
public void setPosition(Position i) { position = i; }
public Position getPosition() { return position; }
@XMLProperty("image")
public void setImageResource(String s) { imageResource = s; }
public String getImageResource() { return imageResource; }
@XMLProperty("images")
public void setImageResources(String[] a) { imageResources = a; }
public String[] getImageResources() { return imageResources; }
@XMLProperty
public void setTrim(Trim[] a) { trim = a; }
public Trim[] getTrim() { return trim; }
@XMLProperty
public void setWidth(SizeMeasure m)
{
if(m == null) m = DEFAULT_SIZE;
width = m;
}
public SizeMeasure getWidth() { return width; }
@XMLProperty
public void setHeight(SizeMeasure m)
{
if(m == null) m = DEFAULT_SIZE;
height = m;
}
public SizeMeasure getHeight() { return height; }
// [TODO] Support "initial" value for minWidth and minHeight
@XMLProperty
public void setMinWidth(SizeMeasure m)
{
if(m == null) m = DEFAULT_SIZE;
minWidth = m;
}
public SizeMeasure getMinWidth() { return minWidth; }
@XMLProperty
public void setMinHeight(SizeMeasure m)
{
if(m == null) m = DEFAULT_SIZE;
minHeight = m;
}
public SizeMeasure getMinHeight() { return minHeight; }
public WindowInstance createWindowInstance(NAFApplication app, ModelMap models, WindowInstance parentWI) throws NAFException
{
Shell parentShell = (parentWI == null) ? null : parentWI.getShell(false);
Display display = app.getDisplay();
ResourceImageManager imageManager = app.getImageManager();
int style = Trim.style(trim) | modality.style;
if(resize) style |= SWT.MAX | SWT.RESIZE;
final Shell shell = parentShell == null ? new Shell(display, style) : new Shell(parentShell, style);
ShellWindowInstance wi = new ShellWindowInstance(this, models, app, shell);
if(title != null) shell.setText(title);
NGComponent menubarComponent = getFirstChild("menubar");
if(menubarComponent != null)
{
if(!(menubarComponent instanceof NGMenu))
throw new NAFException("Only NGMenu objects can be assigned to role \"menubar\" in NGWindow");
Menu bar = ((NGMenu)menubarComponent).createMenuBar(shell, wi);
shell.setMenuBar(bar);
}
if((imageResource != null && imageResource.length() > 0) || imageResources.length != 0)
{
int imgnum = imageResources.length;
if(imageResource != null && imageResource.length() > 0) imgnum++;
final IManagedImage[] managedImages = new IManagedImage[imgnum];
final Image[] images = new Image[imgnum];
for(int i=0; i<imageResources.length; i++)
{
managedImages[i] = imageManager.getMaskRemappedImage(ResourceImageManager.absoluteURIFor(getResourceURL(), imageResources[i]));
images[i] = managedImages[i].acquire();
}
if(imageResources.length != imgnum)
{
managedImages[imgnum-1] = imageManager.getMaskRemappedImage(ResourceImageManager.absoluteURIFor(getResourceURL(), imageResource));
images[imgnum-1] = managedImages[imgnum-1].acquire();
}
shell.setImages(images);
shell.addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{
for(int i=0; i<managedImages.length; i++) managedImages[i].release();
}
});
}
WidgetData wd = createWidgetData();
wd.attachTo(shell);
configureControl(shell, wi, wd, null);
addDefaultChildren(shell, wi, wd);
addCommonFeatures(shell, wi, null);
shell.pack();
// [TODO] Improve min size handling
final int embase = SWTUtil.computeEMBase(shell);
final Point minShellSize = shell.getSize();
if(width.baseSize >= 0 || height.baseSize >= 0)
{
int w = minShellSize.x, h = minShellSize.y;
if(width.baseSize >= 0) w = width.compute(embase, w);
if(height.baseSize >= 0) h = height.compute(embase, h);
shell.setSize(w, h);
}
int computedMinWidth = minWidth.compute(embase);
int computedMinHeight = minHeight.compute(embase);
if(computedMinWidth != SWT.DEFAULT || computedMinHeight != SWT.DEFAULT)
{
Point p = shell.getMinimumSize();
int w = p.x, h = p.y;
if(computedMinWidth != SWT.DEFAULT) w = computedMinWidth;
if(computedMinHeight != SWT.DEFAULT) h = computedMinHeight;
shell.setMinimumSize(w, h);
}
/*if(keepMinimumSize)
{
((WindowWidgetData)WidgetData.forWidget(shell)).minSize = minShellSize;
final boolean[] lock = new boolean[1];
shell.addControlListener(new ControlAdapter()
{
public void controlResized(ControlEvent e)
{
if(lock[0]) return;
if(shell.getMaximized()) return;
lock[0] = true;
try
{
Point size = shell.getSize();
if(size.x < minShellSize.x || size.y < minShellSize.y)
shell.setSize(new Point(Math.max(size.x, minShellSize.x), Math.max(size.y, minShellSize.y)));
}
finally { lock[0] = false; }
}
});
}*/
final IWindowStateModel windowStateModel = getModel("window", wi.models);
int wstate;
if(windowStateModel != null && (wstate = windowStateModel.getState()) != IWindowStateModel.STATE_DEFAULTBOUNDS)
{
Rectangle wbounds = windowStateModel.getPluralizedBounds();
Rectangle dbounds = display.getBounds();
if(wbounds.width > dbounds.width) wbounds.width = dbounds.width;
if(wbounds.height > dbounds.height) wbounds.height = dbounds.height;
if(wbounds.x < dbounds.x) wbounds.x = dbounds.x;
else if(wbounds.x > dbounds.x + dbounds.width - wbounds.width)
wbounds.x = dbounds.x + dbounds.width - wbounds.height;
if(wbounds.y < dbounds.y) wbounds.y = dbounds.y;
else if(wbounds.y > dbounds.y + dbounds.height - wbounds.height)
wbounds.y = dbounds.y + dbounds.height - wbounds.height;
shell.setLocation(wbounds.x, wbounds.y);
shell.setSize(wbounds.width, wbounds.height);
if(wstate == IWindowStateModel.STATE_MAXIMIZED || wstate == IWindowStateModel.STATE_MINIMIZED_AFTER_MAXIMIZED)
shell.setMaximized(true);
}
else if(position != Position.DEFAULT)
{
Rectangle bounds = null;
switch(position)
{
case FULL_DISPLAY:
shell.setBounds(display.getBounds());
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -