下面列出了javax.swing.text.html.ImageView#com.intellij.ide.DataManager 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public BranchActionGroupPopup(@Nonnull String title, @Nonnull Project project, @Nonnull Condition<AnAction> preselectActionCondition, @Nonnull ActionGroup actions, @Nullable String dimensionKey) {
super(title, createBranchSpeedSearchActionGroup(actions), SimpleDataContext.getProjectContext(project), preselectActionCondition, true);
myProject = project;
DataManager.registerDataProvider(getList(), dataId -> POPUP_MODEL == dataId ? getListModel() : null);
myKey = dimensionKey;
if (myKey != null) {
Dimension storedSize = WindowStateService.getInstance(myProject).getSizeFor(myProject, myKey);
if (storedSize != null) {
//set forced size before component is shown
setSize(storedSize);
myUserSizeChanged = true;
}
createTitlePanelToolbar(myKey);
}
myMeanRowHeight = getList().getCellBounds(0, 0).height + UIUtil.getListCellVPadding() * 2;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final AnAction action = ActionManager.getInstance().getAction("Flutter.ExtractWidget");
if (action != null) {
TransactionGuard.submitTransaction(project, () -> {
final Editor editor = getCurrentEditor();
if (editor == null) {
// It is a race condition if we hit this. Gracefully assume
// the action has just been canceled.
return;
}
final JComponent editorComponent = editor.getComponent();
final DataContext editorContext = DataManager.getInstance().getDataContext(editorComponent);
final AnActionEvent editorEvent = AnActionEvent.createFromDataContext(ActionPlaces.UNKNOWN, null, editorContext);
action.actionPerformed(editorEvent);
});
}
}
private VirtualFile[] doAdd() {
VirtualFile baseDir = myAddBaseDir;
Project project = DataManager.getInstance().getDataContext(myComponent).getData(CommonDataKeys.PROJECT);
if (baseDir == null && project != null) {
baseDir = project.getBaseDir();
}
VirtualFile[] files = FileChooser.chooseFiles(myDescriptor, myComponent, project, baseDir);
files = adjustAddedFileSet(myComponent, files);
List<VirtualFile> added = new ArrayList<VirtualFile>(files.length);
for (VirtualFile vFile : files) {
if (addElement(vFile)) {
added.add(vFile);
}
}
return VfsUtil.toVirtualFileArray(added);
}
private void requestModelUpdateFromContextOrObject(DataContext dataContext, Object object) {
try {
final NavBarModel model = myPanel.getModel();
if (dataContext != null) {
if (dataContext.getData(CommonDataKeys.PROJECT) != myPanel.getProject() || myPanel.isNodePopupActive()) {
requestModelUpdate(null, myPanel.getContextObject(), true);
return;
}
final Window window = SwingUtilities.getWindowAncestor(myPanel);
if (window != null && !window.isFocused()) {
model.updateModel(DataManager.getInstance().getDataContext(myPanel));
}
else {
model.updateModel(dataContext);
}
}
else {
model.updateModel(object);
}
queueRebuildUi();
}
finally {
myModelUpdating.set(false);
}
}
@Override
@Nullable
public Object getData(@Nonnull @NonNls Key<?> dataId) {
if (PlatformDataKeys.CONTENT_MANAGER == dataId || PlatformDataKeys.NONEMPTY_CONTENT_MANAGER == dataId && getContentCount() > 1) {
return DesktopContentManagerImpl.this;
}
for (DataProvider dataProvider : myDataProviders) {
Object data = dataProvider.getData(dataId);
if (data != null) {
return data;
}
}
if (myUI instanceof DataProvider) {
return ((DataProvider)myUI).getData(dataId);
}
DataProvider provider = DataManager.getDataProvider(this);
return provider == null ? null : provider.getData(dataId);
}
private void reset(final Keymap keymap, final QuickList[] allQuickLists, String filter, @Nullable KeyboardShortcut shortcut) {
myKeymap = keymap;
final PathsKeeper pathsKeeper = new PathsKeeper();
pathsKeeper.storePaths();
myRoot.removeAllChildren();
ActionManager actionManager = ActionManager.getInstance();
Project project = DataManager.getInstance().getDataContext(myComponent).getData(CommonDataKeys.PROJECT);
Group mainGroup = ActionsTreeUtil.createMainGroup(project, myKeymap, allQuickLists, filter, true, filter != null && filter.length() > 0 ?
ActionsTreeUtil.isActionFiltered(filter, true) :
(shortcut != null ? ActionsTreeUtil.isActionFiltered(actionManager, myKeymap, shortcut) : null));
if ((filter != null && filter.length() > 0 || shortcut != null) && mainGroup.initIds().isEmpty()){
mainGroup = ActionsTreeUtil.createMainGroup(project, myKeymap, allQuickLists, filter, false, filter != null && filter.length() > 0 ?
ActionsTreeUtil.isActionFiltered(filter, false) :
ActionsTreeUtil.isActionFiltered(actionManager, myKeymap, shortcut));
}
myRoot = ActionsTreeUtil.createNode(mainGroup);
myMainGroup = mainGroup;
MyModel model = (MyModel)myTree.getModel();
model.setRoot(myRoot);
model.nodeStructureChanged(myRoot);
pathsKeeper.restorePaths();
}
ActionJButton(final AnAction action) {
super(action.getTemplatePresentation().getText());
myAction = action;
setMnemonic(action.getTemplatePresentation().getMnemonic());
setDisplayedMnemonicIndex(action.getTemplatePresentation().getDisplayedMnemonicIndex());
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AnActionEvent event = new AnActionEvent(null, ((BaseDataManager)DataManager.getInstance()).getDataContextTest(ButtonToolbarImpl.this), myPlace, myPresentationFactory.getPresentation(action),
ActionManager.getInstance(), e.getModifiers());
if (ActionUtil.lastUpdateAndCheckDumb(action, event, false)) {
ActionUtil.performActionDumbAware(action, event);
}
}
});
}
public void actionPerformed(AnActionEvent e) {
ContentManager contentManager = ContentManagerUtil.getContentManagerFromContext(e.getDataContext(), true);
boolean processed = false;
if (contentManager != null && contentManager.canCloseContents()) {
final Content selectedContent = contentManager.getSelectedContent();
if (selectedContent != null && selectedContent.isCloseable()) {
contentManager.removeContent(selectedContent, true);
processed = true;
}
}
if (!processed && contentManager != null) {
final DataContext context = DataManager.getInstance().getDataContext(contentManager.getComponent());
final ToolWindow tw = context.getData(PlatformDataKeys.TOOL_WINDOW);
if (tw != null) {
tw.hide(null);
}
}
}
@Inject
@ReviewAfterMigrationToJRE(9)
public MacOSDefaultMenuInitializer(AboutManager aboutManager, DataManager dataManager, WindowManager windowManager) {
myAboutManager = aboutManager;
myDataManager = dataManager;
myWindowManager = windowManager;
if (SystemInfo.isMac) {
try {
ThrowableRunnable<Throwable> task = SystemInfo.isJavaVersionAtLeast(9) ? new Java9Worker(this::showAbout) : new PreJava9Worker(this::showAbout);
task.run();
installAutoUpdateMenu();
}
catch (Throwable t) {
LOGGER.warn(t);
}
}
}
private ListPopup createLanguagePopup() {
DefaultActionGroup actions = new DefaultActionGroup();
for (Language language : getSupportedLanguages()) {
//noinspection ConstantConditions
actions.add(new AnAction(language.getDisplayName(), null, language.getAssociatedFileType().getIcon()) {
@RequiredUIAccess
@Override
public void actionPerformed(@Nonnull AnActionEvent e) {
XExpression currentExpression = getExpression();
setExpression(new XExpressionImpl(currentExpression.getExpression(), language, currentExpression.getCustomInfo()));
requestFocusInEditor();
}
});
}
DataContext dataContext = DataManager.getInstance().getDataContext(getComponent());
return JBPopupFactory.getInstance().createActionGroupPopup("Choose Language", actions, dataContext,
JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
false);
}
protected static DataContext getCurrentEditorDataContext() {
final DataContext defaultContext = DataManager.getInstance().getDataContext();
return new DataContext() {
@Override
@javax.annotation.Nullable
public Object getData(@NonNls Key dataId) {
if (PlatformDataKeys.EDITOR == dataId) {
return getEditor();
}
if (CommonDataKeys.PROJECT == dataId) {
return getProject();
}
if (LangDataKeys.PSI_FILE == dataId) {
return getFile();
}
if (LangDataKeys.PSI_ELEMENT == dataId) {
PsiFile file = getFile();
if (file == null) return null;
Editor editor = getEditor();
if (editor == null) return null;
return file.findElementAt(editor.getCaretModel().getOffset());
}
return defaultContext.getData(dataId);
}
};
}
/**
* {@inheritDoc}
*/
@Override
public void remind() {
DataManager.getInstance().getDataContextFromFocus()
.doWhenDone((Consumer<DataContext>) (dataContext -> new OpenImageConsumer().accept(dataContext)))
.doWhenRejected((Consumer<String>) LOG::error);
}
@Override
public void show(final Component component, int x, int y) {
if (!component.isShowing()) {
//noinspection HardCodedStringLiteral
throw new IllegalArgumentException("component must be shown on the screen");
}
removeAll();
// Fill menu. Only after filling menu has non zero size.
int x2 = Math.max(0, Math.min(x, component.getWidth() - 1)); // fit x into [0, width-1]
int y2 = Math.max(0, Math.min(y, component.getHeight() - 1)); // fit y into [0, height-1]
myContext = myDataContextProvider != null ? myDataContextProvider.get() : DataManager.getInstance().getDataContext(component, x2, y2);
Utils.fillMenu(myGroup, this, true, myPresentationFactory, myContext, myPlace, false, false, LaterInvocator.isInModalContext());
if (getComponentCount() == 0) {
return;
}
if (myApp != null) {
if (myApp.isActive()) {
Component frame = UIUtil.findUltimateParent(component);
if (frame instanceof Window) {
consulo.ui.Window uiWindow = TargetAWT.from((Window)frame);
myFrame = uiWindow.getUserData(IdeFrame.KEY);
}
myConnection = myApp.getMessageBus().connect();
myConnection.subscribe(ApplicationActivationListener.TOPIC, ActionPopupMenuImpl.this);
}
}
super.show(component, x, y);
}
@Override
public void execute(@Nonnull Editor editor, DataContext dataContext) {
if (LOG.isDebugEnabled()) {
LOG.debug("enter: execute(editor='" + editor + "')");
}
Project project = DataManager.getInstance().getDataContext(editor.getComponent()).getData(CommonDataKeys.PROJECT);
if (project == null) {
if (myOriginalHandler != null) {
myOriginalHandler.execute(editor, dataContext);
}
return;
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
TextRange range = selectWord(editor, project);
if (editor instanceof EditorWindow) {
if (range == null || !isInsideEditableInjection((EditorWindow)editor, range, project) || TextRange.from(0, editor.getDocument().getTextLength()).equals(
new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd()))) {
editor = ((EditorWindow)editor).getDelegate();
range = selectWord(editor, project);
}
}
if (range == null) {
if (myOriginalHandler != null) {
myOriginalHandler.execute(editor, dataContext);
}
}
else {
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
}
}
private void createUIComponents() {
functionMethodNameTextField = new NameSuggestionsField(
new String[] {"fun"},
(Project) DataManager.getInstance().getDataContext().getData(DataConstants.PROJECT),
CppSupportLoader.CPP_FILETYPE
);
}
private JComponent createToolbar() {
JPanel toolBarPanel = new JPanel(new GridLayout());
DefaultActionGroup leftGroup = new DefaultActionGroup();
leftGroup.add(ActionManager.getInstance().getAction(RUN_DASHBOARD_TOOLBAR));
// TODO [konstantin.aleev] provide context help ID
//leftGroup.add(new Separator());
//leftGroup.add(new ContextHelpAction(HELP_ID));
ActionToolbar leftActionToolBar = ActionManager.getInstance().createActionToolbar(PLACE_TOOLBAR, leftGroup, false);
toolBarPanel.add(leftActionToolBar.getComponent());
myTree.putClientProperty(DataManager.CLIENT_PROPERTY_DATA_PROVIDER, new DataProvider() {
@Override
public Object getData(@Nonnull @NonNls Key dataId) {
if (KEY == dataId) {
return RunDashboardContent.this;
}
return null;
}
});
leftActionToolBar.setTargetComponent(myTree);
DefaultActionGroup rightGroup = new DefaultActionGroup();
TreeExpander treeExpander = new DefaultTreeExpander(myTree);
AnAction expandAllAction = CommonActionsManager.getInstance().createExpandAllAction(treeExpander, this);
rightGroup.add(expandAllAction);
AnAction collapseAllAction = CommonActionsManager.getInstance().createCollapseAllAction(treeExpander, this);
rightGroup.add(collapseAllAction);
rightGroup.add(new AnSeparator());
myGroupers.stream().filter(grouper -> !grouper.getRule().isAlwaysEnabled()).forEach(grouper -> rightGroup.add(new GroupAction(grouper)));
ActionToolbar rightActionToolBar = ActionManager.getInstance().createActionToolbar(PLACE_TOOLBAR, rightGroup, false);
toolBarPanel.add(rightActionToolBar.getComponent());
rightActionToolBar.setTargetComponent(myTree);
return toolBarPanel;
}
/**
* This method should get notifications abount changes of focused window.
* Only <code>focusedWindow</code> property is acceptable.
*
* @throws IllegalArgumentException if property name isn't <code>focusedWindow</code>.
*/
@Override
public final void propertyChange(final PropertyChangeEvent e) {
if (LOG.isDebugEnabled()) {
LOG.debug("enter: propertyChange(" + e + ")");
}
if (!FOCUSED_WINDOW_PROPERTY.equals(e.getPropertyName())) {
throw new IllegalArgumentException("unknown property name: " + e.getPropertyName());
}
synchronized (myLock) {
final consulo.ui.Window window = TargetAWT.from((Window)e.getNewValue());
if (window == null || myApplication.isDisposed()) {
return;
}
if (!myWindow2Info.containsKey(window)) {
myWindow2Info.put(window, new WindowInfo(window, true));
}
myFocusedWindow = window;
final Project project = DataManager.getInstance().getDataContext(myFocusedWindow).getData(CommonDataKeys.PROJECT);
for (Iterator<consulo.ui.Window> i = myFocusedWindows.iterator(); i.hasNext(); ) {
final consulo.ui.Window w = i.next();
final DataContext dataContext = DataManager.getInstance().getDataContext(TargetAWT.to(w));
if (project == dataContext.getData(CommonDataKeys.PROJECT)) {
i.remove();
}
}
myFocusedWindows.add(myFocusedWindow);
// Set new root frame
final IdeFrame frame = IdeFrameUtil.findRootIdeFrame(window);
if (frame != null) {
JOptionPane.setRootFrame((Frame)TargetAWT.to(frame.getWindow()));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("exit: propertyChange()");
}
}
public BaseLabel(DesktopToolWindowContentUi ui, boolean bold) {
myUi = ui;
setOpaque(false);
myBold = bold;
DataManager.registerDataProvider(this, dataId -> {
if (dataId == ToolWindowDataKeys.CONTENT) {
return getContent();
}
return null;
});
}
@Nonnull
@RequiredUIAccess
public static ConfigurationContext getFromContext(DataContext dataContext) {
final ConfigurationContext context = new ConfigurationContext(dataContext);
final DataManager dataManager = DataManager.getInstance();
ConfigurationContext sharedContext = dataManager.loadFromDataContext(dataContext, SHARED_CONTEXT);
if (sharedContext == null ||
sharedContext.getLocation() == null ||
context.getLocation() == null ||
!Comparing.equal(sharedContext.getLocation().getPsiElement(), context.getLocation().getPsiElement())) {
sharedContext = context;
dataManager.saveInDataContext(dataContext, SHARED_CONTEXT, sharedContext);
}
return sharedContext;
}
private Optional<DataContext> getDataContext() {
try {
return Optional.ofNullable(
DataManager.getInstance()
.getDataContextFromFocusAsync()
.blockingGet(100, TimeUnit.MILLISECONDS));
} catch (TimeoutException | ExecutionException e) {
return Optional.empty();
}
}
private static Component discoverPopup(final Key<JBPopup> datakey, Component focusOwner) {
if (focusOwner == null) {
focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
}
if (focusOwner == null) return null;
final DataContext dataContext = DataManager.getInstance().getDataContext(focusOwner);
final JBPopup popup = dataContext.getData(datakey);
if (popup != null && popup.isVisible() && !popup.isDisposed()) {
return popup.getContent();
}
return null;
}
private void scrollToSource(Component tree) {
DataContext dataContext = DataManager.getInstance().getDataContext(tree);
getReady(dataContext)
.doWhenDone(
() ->
TransactionGuard.submitTransaction(
ApplicationManager.getApplication(),
() -> {
DataContext context = DataManager.getInstance().getDataContext(tree);
Navigatable navigatable = BLAZE_CONSOLE_NAVIGATABLE_DATA_KEY.getData(context);
if (navigatable != null) {
OpenSourceUtil.navigate(false, true, navigatable);
}
}));
}
private static boolean isApplicable(PsiFile file, DataContext dataContext) {
if (!(file instanceof ProjectViewPsiFile)) {
return false;
}
Boolean isSplitLine =
DataManager.getInstance().loadFromDataContext(dataContext, SplitLineAction.SPLIT_LINE_KEY);
if (isSplitLine != null) {
return false;
}
return true;
}
private static boolean isApplicable(PsiFile file, DataContext dataContext) {
if (!(file instanceof BuildFile)) {
return false;
}
Boolean isSplitLine =
DataManager.getInstance().loadFromDataContext(dataContext, SplitLineAction.SPLIT_LINE_KEY);
if (isSplitLine != null) {
return false;
}
return true;
}
private void checkUpdate() {
if (myProject.isDisposed()) return;
final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
final boolean insideToolwindow = SwingUtilities.isDescendingFrom(myToolWindow.getComponent(), owner);
if (!myFirstRun && (insideToolwindow || JBPopupFactory.getInstance().isPopupActive())) {
return;
}
final DataContext dataContext = DataManager.getInstance().getDataContext(owner);
if (dataContext.getData(ourDataSelectorKey) == this) return;
if (dataContext.getData(CommonDataKeys.PROJECT) != myProject) return;
final VirtualFile[] files = hasFocus() ? null : dataContext.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
if (!myToolWindow.isVisible()) {
if (files != null && files.length > 0) {
myFile = files[0];
}
return;
}
if (files != null && files.length == 1) {
setFile(files[0]);
}
else if (files != null && files.length > 1) {
setFile(null);
} else if (myFirstRun) {
final FileEditorManagerImpl editorManager = (FileEditorManagerImpl)FileEditorManager.getInstance(myProject);
final List<Pair<VirtualFile, EditorWindow>> history = editorManager.getSelectionHistory();
if (! history.isEmpty()) {
setFile(history.get(0).getFirst());
}
}
myFirstRun = false;
}
/**
* To support the custom language features, we need a ProjectImpl, and it's not desirable to
* create one from scratch.<br>
*
* @return the current, non-default project, if one exists, else the default project.
*/
public static Project getProject() {
Project project = (Project) DataManager.getInstance().getDataContext().getData("project");
if (project != null && project instanceof ProjectImpl) {
return project;
}
return ProjectManager.getInstance().getDefaultProject();
}
@Override
@Nonnull
protected JComponent createEditor() {
JPanel wholePanel = new JPanel(new BorderLayout());
wholePanel.add(myEditor.getComponent(), BorderLayout.NORTH);
wholePanel.add(myBeforeLaunchContainer, BorderLayout.SOUTH);
DataManager.registerDataProvider(wholePanel, new TypeSafeDataProviderAdapter(new MyDataProvider()));
return wholePanel;
}
@Override
public void actionPerformed(@Nonnull AnActionEvent e) {
setExpanded(!myIsExpanded);
InputEvent event = e.getInputEvent();
if (event != null && event.getSource() instanceof JComponent) {
DataProvider dataProvider = DataManager.getDataProvider((JComponent)event.getSource());
if (dataProvider != null) {
assertNotNull(dataProvider.getDataUnchecked(POPUP_MODEL)).refilter();
}
}
}
public KeyboardShortcutDialog(Component component, String actionId, final QuickList[] quickLists) {
super(component, true);
setTitle(KeyMapBundle.message("keyboard.shortcut.dialog.title"));
myActionId = actionId;
final Project project = DataManager.getInstance().getDataContext(component).getData(CommonDataKeys.PROJECT);
myMainGroup = ActionsTreeUtil.createMainGroup(project, myKeymap, quickLists, null, false, null); //without current filter
myEnableSecondKeystroke = new JCheckBox();
UIUtil.applyStyle(UIUtil.ComponentStyle.SMALL, myEnableSecondKeystroke);
myEnableSecondKeystroke.setBorder(new EmptyBorder(4, 0, 0, 2));
myEnableSecondKeystroke.setFocusable(false);
myKeystrokePreview = new JLabel(" ");
myConflictInfoArea = new JTextArea("");
myConflictInfoArea.setFocusable(false);
init();
}
public MacrosDialog(Component parent) {
super(parent, true);
MacroManager.getInstance().cacheMacrosPreview(DataManager.getInstance().getDataContext(parent));
setTitle(IdeBundle.message("title.macros"));
setOKButtonText(IdeBundle.message("button.insert"));
myMacrosModel = new DefaultListModel();
myMacrosList = new JBList(myMacrosModel);
myPreviewTextarea = new JTextArea();
init();
}