📄 speedscaleshell.java
字号:
/* * JMule - Java file sharing client * Copyright (C) 2007-2008 JMule team ( jmule@jmule.org / http://jmule.org ) * * Any parts of this program derived from other projects, or contributed * by third-party developers are copyrighted by their respective authors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 org.jmule.ui.swt.common;import java.util.Iterator;import java.util.LinkedHashMap;import org.eclipse.swt.SWT;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.KeyListener;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.events.MouseTrackListener;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.events.TraverseEvent;import org.eclipse.swt.events.TraverseListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Monitor;import org.eclipse.swt.widgets.Shell;import org.jmule.core.JMConstants;import org.jmule.ui.swt.SWTThread;import org.jmule.ui.utils.SpeedFormatter;/** * Cheap ugly slider shell * Created on Aug 22, 2008 * @author TuxPaper * @author binary256 * @version $Revision: 1.1 $ * Last changed by $Author: binary256_ $ on $Date: 2008/09/07 16:40:14 $ */public class SpeedScaleShell { private static final boolean MOUSE_ONLY_UP_EXITS = true; private static final int OPTION_HEIGHT = 15; private static final int TEXT_HEIGHT = 32; private static final int SCALER_HEIGHT = 20; private int HEIGHT = TEXT_HEIGHT + SCALER_HEIGHT; private static final int WIDTH = 120; private static final int PADDING_X0 = 10; private static final int PADDING_X1 = 10; private static final int WIDTH_NO_PADDING = WIDTH - PADDING_X0 - PADDING_X1; private static final int TYPED_TEXT_ALPHA = 80; private static final long CLOSE_DELAY = 600; private long value; private boolean cancelled; private long minValue; private long maxValue; private long maxTextValue; private long pageIncrement; private long bigPageIncrement; private Shell shell; private LinkedHashMap mapOptions = new LinkedHashMap(); private String sValue = ""; private String title = "Jora"; private Composite composite; private boolean menuChosen; protected boolean lastMoveHadMouseDown; private boolean assumeInitiallyDown; public static void main(String[] args) { SpeedScaleShell speedScaleWidget = new SpeedScaleShell("Test") { public String getStringValue() { return getValue() + "b/s"; } }; speedScaleWidget.setMaxValue(10000); speedScaleWidget.setMaxTextValue(15000); speedScaleWidget.addOption("AutoSpeed", -1); speedScaleWidget.addOption("Preset: 10b/s", 10);// speedScaleWidget.addOption("Preset: 20b/s", 20);// speedScaleWidget.addOption("Preset: 1b/s", 1);// speedScaleWidget.addOption("Preset: 1000b/s", 1000); speedScaleWidget.addOption("Preset: A really long preset", 2000); System.out.println("returns " + speedScaleWidget.open(1000, JMConstants.isWindows) + " w/" + speedScaleWidget.getValue()); } public SpeedScaleShell(String title) { minValue = 0; maxValue = -1; maxTextValue = -1; pageIncrement = 10; bigPageIncrement = 100; cancelled = true; menuChosen = false; this.title = title; } /** * Borks with 0 or -1 maxValue * * @param startValue * @param assumeInitiallyDown * @return * * @since 3.0.1.7 */ public boolean open(final long startValue, boolean _assumeInitiallyDown) { value = startValue; this.assumeInitiallyDown = _assumeInitiallyDown; cancelled = true; shell = new Shell(SWTThread.getDisplay(), SWT.DOUBLE_BUFFERED | SWT.ON_TOP); shell.setLayout(new FillLayout()); final Display display = shell.getDisplay(); composite = new Composite(shell, SWT.DOUBLE_BUFFERED); final Point firstMousePos = display.getCursorLocation(); composite.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_ESCAPE) { setCancelled(true); shell.dispose(); } else if (e.detail == SWT.TRAVERSE_ARROW_NEXT) { setValue(value + 1); } else if (e.detail == SWT.TRAVERSE_ARROW_PREVIOUS) { setValue(value - 1); } else if (e.detail == SWT.TRAVERSE_PAGE_NEXT) { setValue(value + bigPageIncrement); } else if (e.detail == SWT.TRAVERSE_PAGE_PREVIOUS) { setValue(value - bigPageIncrement); } else if (e.detail == SWT.TRAVERSE_RETURN) { setCancelled(false); shell.dispose(); } } }); composite.addKeyListener(new KeyListener() { public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { if (e.keyCode == SWT.PAGE_DOWN && e.stateMask == 0) { setValue(value + pageIncrement); } else if (e.keyCode == SWT.PAGE_UP && e.stateMask == 0) { setValue(value - pageIncrement); } else if (e.keyCode == SWT.HOME) { setValue(minValue); } else if (e.keyCode == SWT.END) { if (maxValue != -1) { setValue(maxValue); } } } }); composite.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent e) { lastMoveHadMouseDown = false; boolean hasButtonDown = (e.stateMask & SWT.BUTTON_MASK) > 0 || assumeInitiallyDown; if (hasButtonDown) { if (e.y > HEIGHT - SCALER_HEIGHT) { lastMoveHadMouseDown = true; setValue(getValueFromMousePos(e.x)); } composite.redraw(); } else { composite.redraw(); } } }); composite.addMouseTrackListener(new MouseTrackListener() { boolean mouseIsOut = false; private boolean exitCancelled = false; public void mouseHover(MouseEvent e) { } public void mouseExit(MouseEvent e) { if (!exitCancelled) { shell.dispose(); } else { exitCancelled = false; } } public void mouseEnter(MouseEvent e) { if (mouseIsOut) { exitCancelled = true; } mouseIsOut = false; } }); composite.addMouseListener(new MouseListener() { boolean bMouseDown = false; public void mouseUp(MouseEvent e) { if (assumeInitiallyDown) { assumeInitiallyDown = false; } if (MOUSE_ONLY_UP_EXITS) { if (lastMoveHadMouseDown) { Point mousePos = display.getCursorLocation(); if (mousePos.equals(firstMousePos)) { lastMoveHadMouseDown = false; return; } } bMouseDown = true; } if (bMouseDown) { if (e.y > HEIGHT - SCALER_HEIGHT) { setValue(getValueFromMousePos(e.x)); setCancelled(false); if (lastMoveHadMouseDown) { shell.dispose(); } } else if (e.y > TEXT_HEIGHT) { int idx = (e.y - TEXT_HEIGHT) / OPTION_HEIGHT; Iterator iterator = mapOptions.keySet().iterator(); long newValue; do { newValue = ((Long) iterator.next()).intValue(); idx--; } while (idx >= 0); value = newValue; // ignore min/max setCancelled(false); setMenuChosen(true); shell.dispose(); } } } public void mouseDown(MouseEvent e) { if (e.count > 1) { lastMoveHadMouseDown = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -