📄 swtbrowser.java
字号:
/*
* Copyright (c) 2007 Software Wizards Pty Ltd, Victoria, Australia.
* mailto:enquires@swz.com.au. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted. See the GNU General Public License
* for more details.
*
* Redistribution of the SWTtoCOM software is not permitted as part of any
* commercial product. Licensing terms for such distribution may be
* obtained from the copyright holder.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package au.com.swz.swttocom.example.browser;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import au.com.swz.swttocom.example.browser.actions.CopyAction;
import au.com.swz.swttocom.example.browser.actions.GoBackAction;
import au.com.swz.swttocom.example.browser.actions.GoForwardAction;
import au.com.swz.swttocom.example.browser.actions.GoHomeAction;
import au.com.swz.swttocom.example.browser.actions.GoSearchAction;
import au.com.swz.swttocom.example.browser.actions.NewBrowserTabAction;
import au.com.swz.swttocom.example.browser.actions.PrintAction;
import au.com.swz.swttocom.example.browser.actions.PrintPreviewAction;
import au.com.swz.swttocom.example.browser.actions.RefreshAction;
import au.com.swz.swttocom.example.browser.actions.SaveAsAction;
import au.com.swz.swttocom.example.browser.actions.StopAction;
public class SWTBrowser extends Composite {
private static final Image TAB_IMAGE = ImageDescriptor.createFromFile(
SWTBrowser.class, "actions/images/globe.gif").createImage(); //$NON-NLS-1$
private CTabFolder tabFolder;
private GoBackAction goBackAction;
private GoForwardAction goForwardAction;
private StatusBar statusBar;
private AddressBar addressBar;
public SWTBrowser(Composite parent, int style) {
super(parent, style);
Display display = Display.getDefault();
GridLayout thisLayout = new GridLayout(1, true);
thisLayout.marginWidth = 0;
thisLayout.marginHeight = 0;
thisLayout.numColumns = 1;
thisLayout.makeColumnsEqualWidth = true;
thisLayout.horizontalSpacing = 0;
thisLayout.verticalSpacing = 0;
this.setLayout(thisLayout);
ToolBarManager tbManager = new ToolBarManager(SWT.FLAT);
tbManager.add(new NewBrowserTabAction(this));
tbManager.add(new Separator());
goBackAction = new GoBackAction(this);
tbManager.add(goBackAction);
goForwardAction = new GoForwardAction(this);
tbManager.add(goForwardAction);
tbManager.add(new StopAction(this));
tbManager.add(new RefreshAction(this));
tbManager.add(new GoHomeAction(this));
tbManager.add(new Separator());
tbManager.add(new GoSearchAction(this));
tbManager.add(new PrintAction(this));
tbManager.add(new PrintPreviewAction(this));
tbManager.add(new CopyAction(this));
tbManager.add(new SaveAsAction(this));
ToolBar toolbar = tbManager.createControl(this);
toolbar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
addressBar = new AddressBar(this);
Control addressBarControl = addressBar.createComposite(this);
addressBarControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
tabFolder = new CTabFolder(this, SWT.TOP);
tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
tabFolder.setSimple(false);
tabFolder.setSelectionBackground(new Color[] {
display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND),
display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT) },
new int[] { 90 }, true);
tabFolder.setSelectionForeground(display
.getSystemColor(SWT.COLOR_TITLE_FOREGROUND));
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
BrowserTabItem tabItem = getSelection();
if (tabItem != null) {
enableBack(tabItem.isBackEnabled());
enableForward(tabItem.isForwardEnabled());
setAddressText(tabItem.getAddress());
} else {
enableBack(false);
enableForward(false);
setAddressText("");
}
}
});
statusBar = new StatusBar();
Control statusBarControl = statusBar.createComposite(this);
statusBarControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
openNewTab();
}
public BrowserTabItem openNewTab() {
CTabItem tabItem = new CTabItem(tabFolder, SWT.CLOSE);
tabFolder.setSelection(tabItem);
tabItem.setText("");
BrowserTabItem browser = new BrowserTabItem(this, tabItem);
tabItem.setControl(browser.createComposite(tabFolder));
tabItem.setImage(TAB_IMAGE);
tabItem.addListener(SWT.Dispose, new Listener() {
public void handleEvent(Event e) {
CTabItem tabItem = (CTabItem)e.widget;
tabItem.getControl().dispose();
}
});
tabItem.setData(browser);
browser.goHome();
return browser;
}
public BrowserTabItem getSelection() {
CTabItem tabItem = tabFolder.getSelection();
if (tabItem == null) {
return null;
}
return (BrowserTabItem) tabItem.getData();
}
void enableBack(boolean enable) {
goBackAction.setEnabled(enable);
}
void enableForward(boolean enable) {
goForwardAction.setEnabled(enable);
}
void setStatusText(String text) {
statusBar.setText(text);
}
void setProgress(int progress, int progressMax) {
statusBar.setProgress(progress, progressMax);
}
void setAddressText(String text) {
addressBar.setAddress(text);
}
public static void main(String[] args) {
try {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setText("SWTtoCOM Browser");
new SWTBrowser(shell, SWT.NULL);
shell.setLayout(new FillLayout());
Rectangle shellBounds = shell.computeTrim(0, 0, 800, 600);
shell.setSize(shellBounds.width, shellBounds.height);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
TAB_IMAGE.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -