📄 editortoolbar.java
字号:
/*
* Copyright 2006 Pavel Jbanov.
*
* 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 com.jpavel.gwt.wysiwyg.client;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.jpavel.gwt.wysiwyg.client.events.ImageAttached;
import com.queplix.core.client.app.vo.MemoFieldMeta;
import com.queplix.core.client.common.event.Event;
import com.queplix.core.client.common.event.EventListener;
import com.queplix.core.client.common.event.EventSource;
import com.queplix.core.client.common.ui.DialogHelper;
import com.queplix.core.client.common.ui.OkayCancelPopup;
import com.queplix.core.client.common.ui.upload.QFileUpload;
import com.queplix.core.client.common.ui.upload.QFileUploadListener;
import com.queplix.core.client.i18n.I18N;
public class EditorToolbar extends Composite {
private static final String CONFIRMATION_MESSAGE = I18N.getMessages().memoHTMLtoPlaintextConfirmation();
final private Editor editor;
private FlowPanel fullToolbar;
private FlowPanel shortToolbar;
private VerticalPanel topContainer;
private EditorColorPicker fgPicker;
private EditorColorPicker bgPicker;
private OkayCancelPopup fileUploadPopup;
private QFileUpload fileUpload;
private int memoType;
private boolean hasRecords;
private OkayCancelPopup insertLinkPopup;
private Widget insertLinkPanel;
private boolean isAddLinkAsAttachment;
private String imageAttachmentAction;
// -------------------- public events ------------------------
public static interface Events {
Event PANEL_CHANGE = new Event();
Event IMAGE_ATTACHED = new Event();
}
private EventSource eventSource = new EventSource(this);
public EventSource getEventSource() {
return eventSource;
}
// ----------------- end of public events --------------------
private void initInsertLinkPanel() {
if (insertLinkPopup != null) {
return;
}
insertLinkPopup = new OkayCancelPopup("Insert Link");
insertLinkPanel = new VerticalPanel();
HorizontalPanel hp = new HorizontalPanel();
Label nameLabel = new Label("Name");
nameLabel.setWidth(35 + "px");
hp.add(nameLabel);
hp.setCellVerticalAlignment(nameLabel, HasVerticalAlignment.ALIGN_MIDDLE);
final TextBox name = new TextBox();
name.setWidth(200 + "px");
hp.add(name);
HorizontalPanel hp2 = new HorizontalPanel();
Label urlLabel = new Label("URL");
urlLabel.setWidth(35 + "px");
hp2.add(urlLabel);
hp2.setCellVerticalAlignment(urlLabel, HasVerticalAlignment.ALIGN_MIDDLE);
final TextBox url = new TextBox();
url.setWidth(200 + "px");
hp2.add(url);
((VerticalPanel) insertLinkPanel).add(hp);
((VerticalPanel) insertLinkPanel).add(hp2);
insertLinkPopup.setWidget(insertLinkPanel);
insertLinkPopup.getEventSource().addEventListener(new EventListener() {
public void onEvent(Event event, Widget sender) {
if (event == OkayCancelPopup.Events.OK) {
String linkName = name.getText();
String link = url.getText();
if (! link.startsWith("http://")) {
link = "http://" + link;
}
insertLinkPopup.hide();
// pasteHTML does work not for FF when IFRAME is in popup; use appendHTML instead:
link = "<a href='" + link + "' target='_blank'>" + linkName + "</a>";
if (!EditorUtils.isGecko()) {
editor.pasteHTML(link);
} else {
editor.appendHTML(link);
}
} else if (event == OkayCancelPopup.Events.CANCEL) {
insertLinkPopup.hide();
}
}
});
}
private void initFileUpload() {
if (fileUploadPopup != null) {
return;
}
if (imageAttachmentAction == null) {
imageAttachmentAction = QFileUpload.DEFAULT_ACTION;
}
fileUpload = new QFileUpload(imageAttachmentAction, false);
fileUpload.addQFileUploadListener(new QFileUploadListener() {
public void onFileUploaded(Widget sender, String fileName) {
String url = createURL(fileName);
String attachmentSrc = "";
if (isAddLinkAsAttachment) {
String fname = getFileName(fileUpload.getFileUploadName());
attachmentSrc = "<--src=\"cid:" + fname + "\"";
Events.IMAGE_ATTACHED.setData(new ImageAttached(fileName, fname));
eventSource.fireEvent(Events.IMAGE_ATTACHED);
} else {
attachmentSrc = "<--notAttachment";
}
String html = " <img src=\"" + url + "\" " + attachmentSrc + "/>";
// pasteHTML does work not for FF when IFRAME is in popup; use appendHTML instead:
if (!EditorUtils.isGecko()) {
editor.pasteHTML(html);
} else {
editor.appendHTML(html);
}
fileUploadPopup.hide();
}
});
fileUploadPopup = new OkayCancelPopup("Insert Image", true);
fileUploadPopup.getEventSource().addEventListener(new EventListener() {
public void onEvent(Event event, Widget sender) {
if (event == OkayCancelPopup.Events.OK) {
fileUpload.submitUpload();
} else if (event == OkayCancelPopup.Events.CANCEL) {
fileUploadPopup.hide();
}
}
});
fileUploadPopup.setWidget(fileUpload);
}
private String getFileName(String text) {
int index = -1;
if ( (index = text.lastIndexOf('/')) == -1) {
index = text.lastIndexOf('\\');
}
return index == -1 ? text : text.substring(index + 1);
}
private String createURL(String fileName) {
return isAddLinkAsAttachment ? "../attachmentUpload/getTemp?id=" + fileName : "../fileUpload/get?filepath=" + fileName;
}
public EditorToolbar(Editor _editor) {
topContainer = new VerticalPanel();
topContainer.setStyleName("Editor-Toolbar");
topContainer.setWidth("100%");
this.editor = _editor;
fullToolbar = new FlowPanel();
shortToolbar = new FlowPanel();
EditorToolbarButton source = new EditorToolbarButton(EditorToolbarButton.BUTTON_PLAIN_TEXT);
source.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if(memoType == MemoFieldMeta.EDIT_TYPE ||
(memoType != MemoFieldMeta.EDIT_TYPE && !hasRecords)) {
if (!editor.getEditorWYSIWYG().getHTML().trim().equals("") &&
DialogHelper.showModalQuestionDialog(CONFIRMATION_MESSAGE) == DialogHelper.NO) {
return;
}
editor.getEditorWYSIWYG().toggleView();
if(memoType != MemoFieldMeta.EDIT_TYPE) {
eventSource.fireEvent(Events.PANEL_CHANGE);
}
}
}
});
EditorToolbarButton removeFormat = new SimpleCommandButton(EditorToolbarButton.BUTTON_DELETE, "RemoveFormat");
EditorToolbarButton undo = new SimpleCommandButton(EditorToolbarButton.BUTTON_UNDO, "Undo");
EditorToolbarButton redo = new SimpleCommandButton(EditorToolbarButton.BUTTON_REDO, "Redo");
EditorToolbarButton bold = new SimpleCommandButton(EditorToolbarButton.BUTTON_BOLD, "Bold");
EditorToolbarButton italic = new SimpleCommandButton(EditorToolbarButton.BUTTON_ITALIC, "Italic");
EditorToolbarButton underlined = new SimpleCommandButton(EditorToolbarButton.BUTTON_UNDERLINE, "Underline");
EditorToolbarButton subscript = new SimpleCommandButton(EditorToolbarButton.BUTTON_SUBSCRIPT, "Subscript");
EditorToolbarButton superscript = new SimpleCommandButton(EditorToolbarButton.BUTTON_SUPERSCRIPT, "Superscript");
EditorToolbarButton justifyLeft = new SimpleCommandButton(EditorToolbarButton.BUTTON_ALIGNLEFT, "JustifyLeft");
EditorToolbarButton justifyCenter = new SimpleCommandButton(EditorToolbarButton.BUTTON_ALIGNCENTER, "JustifyCenter");
EditorToolbarButton justifyRight = new SimpleCommandButton(EditorToolbarButton.BUTTON_ALIGNRIGHT, "JustifyRight");
EditorToolbarButton justifyJustify = new SimpleCommandButton(EditorToolbarButton.BUTTON_ALIGNJUSTIFY, "JustifyFull");
EditorToolbarButton ol = new SimpleCommandButton(EditorToolbarButton.BUTTON_OL, "InsertOrderedList");
EditorToolbarButton ul = new SimpleCommandButton(EditorToolbarButton.BUTTON_UL, "InsertUnorderedList");
EditorToolbarButton link = new EditorToolbarButton(EditorToolbarButton.BUTTON_LINK);
link.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (!editor.getEditorWYSIWYG().isFrameFocused()) {
EditorUtils.doFocus(editor.getEditorWYSIWYG().getFrame().getElement());
}
EditorUtils.saveSelection(editor.getEditorWYSIWYG().getFrame().getElement());
initInsertLinkPanel();
insertLinkPopup.show();
}
});
EditorToolbarButton unlink = new SimpleCommandButton(EditorToolbarButton.BUTTON_UNLINK, "UnLink");
final EditorToolbarButton image = new EditorToolbarButton(EditorToolbarButton.BUTTON_IMAGE);
image.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (!editor.getEditorWYSIWYG().isFrameFocused()) {
EditorUtils.doFocus(editor.getEditorWYSIWYG().getFrame().getElement());
}
EditorUtils.saveSelection(editor.getEditorWYSIWYG().getFrame().getElement());
initFileUpload();
fileUploadPopup.show();
}
});
EditorToolbarButton foreColor = new EditorToolbarButton(EditorToolbarButton.BUTTON_TEXTCOLOR);
foreColor.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
EditorUtils.saveSelection(editor.getEditorWYSIWYG().getFrame().getElement());
initFgPicker();
fgPicker.show();
fgPicker.setPopupPosition(editor.getAbsoluteLeft() + 50, editor.getAbsoluteTop() + 50);
}
});
EditorToolbarButton bgColor = new EditorToolbarButton(EditorToolbarButton.BUTTON_TEXTBACKGROUNDCOLOR);
bgColor.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
EditorUtils.saveSelection(editor.getEditorWYSIWYG().getFrame().getElement());
initBgPicker();
bgPicker.show();
bgPicker.setPopupPosition(editor.getAbsoluteLeft() + 50, editor.getAbsoluteTop() + 50);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -