-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
379 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 104 additions & 2 deletions
106
...ages/jetbrains-plugin/src/main/kotlin/com/mongodb/jbplugin/editor/MdbJavaEditorToolbar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,116 @@ | ||
package com.mongodb.jbplugin.editor | ||
|
||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.openapi.ui.ComboBox | ||
import com.intellij.openapi.ui.asSequence | ||
import com.intellij.sql.indexOf | ||
import com.intellij.ui.AnimatedIcon.ANIMATION_IN_RENDERER_ALLOWED | ||
import com.intellij.ui.components.JBLabel | ||
import com.intellij.ui.components.JBPanel | ||
import com.mongodb.jbplugin.accessadapter.datagrip.adapter.isConnected | ||
import com.mongodb.jbplugin.i18n.Icons | ||
import com.mongodb.jbplugin.i18n.Icons.scaledToText | ||
import com.mongodb.jbplugin.i18n.MdbToolbarMessages | ||
import java.awt.BorderLayout | ||
import java.awt.event.ItemEvent.DESELECTED | ||
import javax.swing.DefaultComboBoxModel | ||
import javax.swing.SwingConstants | ||
|
||
/** | ||
* Represents the toolbar that will be inserted into an active Java editor. | ||
*/ | ||
class MdbJavaEditorToolbar : JBPanel<MdbJavaEditorToolbar>(BorderLayout()) { | ||
class MdbJavaEditorToolbar( | ||
private val onDataSourceSelected: DataSourceSelectedListener, | ||
private val onDataSourceUnselected: DataSourceUnselectedListener, | ||
) : JBPanel<MdbJavaEditorToolbar>(BorderLayout()) { | ||
var connecting: Boolean = false | ||
var failedConnection: LocalDataSource? = null | ||
|
||
private val connectionComboBox = | ||
ComboBox( | ||
DefaultComboBoxModel( | ||
emptyArray<LocalDataSource>(), | ||
), | ||
) | ||
|
||
init { | ||
add(JBLabel("Placeholder")) | ||
add(connectionComboBox, BorderLayout.EAST) | ||
connectionComboBox.addItemListener { | ||
failedConnection = null | ||
connecting = false | ||
|
||
if (it.stateChange == DESELECTED) { | ||
onDataSourceUnselected() | ||
} else { | ||
onDataSourceSelected(it.item as LocalDataSource) | ||
} | ||
} | ||
connectionComboBox.putClientProperty(ANIMATION_IN_RENDERER_ALLOWED, true) | ||
connectionComboBox.setRenderer { _, value, index, _, _ -> | ||
if (value == null && index == -1) { | ||
JBLabel(MdbToolbarMessages.message("attach.datasource.to.editor"), Icons.Logo.scaledToText(), SwingConstants.LEFT) | ||
} else if (value == null) { | ||
JBLabel(MdbToolbarMessages.message("detach.datasource.from.editor"), Icons.Remove.scaledToText(), SwingConstants.LEFT) | ||
} else { | ||
val icon = | ||
if (value.isConnected()) { | ||
Icons.LogoConnected.scaledToText() | ||
} else if (connecting) { | ||
Icons.LoadingIcon.scaledToText() | ||
} else if (failedConnection?.uniqueId == value.uniqueId) { | ||
Icons.ConnectionFailed.scaledToText() | ||
} else { | ||
Icons.Logo.scaledToText() | ||
} | ||
JBLabel(value.name, icon, SwingConstants.LEFT) | ||
} | ||
} | ||
} | ||
|
||
var dataSources: List<LocalDataSource> | ||
set(value) { | ||
failedConnection = null | ||
connecting = false | ||
|
||
val selectedItem = this.selectedDataSource?.uniqueId | ||
val model = connectionComboBox.model as DefaultComboBoxModel<LocalDataSource> | ||
model.removeAllElements() | ||
model.addElement(null) | ||
model.addAll(value) | ||
selectDataSourceWithId(selectedItem) | ||
} | ||
get() = (connectionComboBox.model as DefaultComboBoxModel<LocalDataSource>).asSequence().toList().filterNotNull() | ||
|
||
var selectedDataSource: LocalDataSource? | ||
set(value) { | ||
failedConnection = null | ||
connecting = false | ||
|
||
if (value == null) { | ||
connectionComboBox.selectedItem = null | ||
} else { | ||
connectionComboBox.selectedItem = value | ||
} | ||
} | ||
get() = connectionComboBox.selectedItem as? LocalDataSource | ||
|
||
private fun selectDataSourceWithId(id: String?) { | ||
if (id == null) { | ||
connectionComboBox.selectedItem = null | ||
} else { | ||
val dataSourceIndex = | ||
(connectionComboBox.model as DefaultComboBoxModel<LocalDataSource?>) | ||
.asSequence() | ||
.toList() | ||
.indexOf { it?.uniqueId == id } | ||
if (dataSourceIndex == -1) { | ||
connectionComboBox.selectedItem = null | ||
} else { | ||
connectionComboBox.selectedIndex = dataSourceIndex | ||
} | ||
} | ||
} | ||
} | ||
|
||
typealias DataSourceSelectedListener = (LocalDataSource) -> Unit | ||
typealias DataSourceUnselectedListener = () -> Unit |
28 changes: 28 additions & 0 deletions
28
...lugin/src/main/kotlin/com/mongodb/jbplugin/editor/MongoDbVirtualFileDataSourceProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.mongodb.jbplugin.editor | ||
|
||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.database.psi.DbDataSource | ||
import com.intellij.database.psi.DbPsiFacade | ||
import com.intellij.database.util.VirtualFileDataSourceProvider | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.Key | ||
import com.intellij.openapi.vfs.VirtualFile | ||
|
||
class MongoDbVirtualFileDataSourceProvider : VirtualFileDataSourceProvider() { | ||
/** | ||
* This needs to be synchronised with the EditorToolbarDecorator field with the same name. | ||
* | ||
* @see EditorToolbarDecorator | ||
*/ | ||
private val attachedDataSource: Key<LocalDataSource> = Key.create("com.mongodb.jbplugin.AttachedDataSource") | ||
|
||
override fun getDataSource( | ||
project: Project, | ||
file: VirtualFile, | ||
): DbDataSource? { | ||
val facade = DbPsiFacade.getInstance(project) | ||
val attachedDataSource = file.getUserData(attachedDataSource) ?: return null | ||
|
||
return facade.findDataSource(attachedDataSource.uniqueId) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/jetbrains-plugin/src/main/kotlin/com/mongodb/jbplugin/i18n/Icons.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.mongodb.jbplugin.i18n | ||
|
||
import com.intellij.icons.AllIcons | ||
import com.intellij.ide.ui.NotRoamableUiSettings | ||
import com.intellij.openapi.util.IconLoader | ||
import com.intellij.ui.AnimatedIcon | ||
import com.intellij.ui.LayeredIcon | ||
import com.intellij.util.IconUtil | ||
import java.awt.* | ||
import javax.swing.Icon | ||
import javax.swing.SwingConstants | ||
|
||
object Icons { | ||
val LoadingIcon = AnimatedIcon.Default() | ||
val GreenCircle = IconLoader.getIcon("/icons/GreenCircle.svg", javaClass) | ||
|
||
val Logo = AllIcons.Providers.MongoDB | ||
val LogoConnected = | ||
LayeredIcon.layeredIcon(arrayOf(Logo, GreenCircle)).apply { | ||
val scaledGreenCircle = IconUtil.resizeSquared(GreenCircle, 6) | ||
setIcon(scaledGreenCircle, 1, SwingConstants.SOUTH_EAST) | ||
} | ||
|
||
val ConnectionFailed = AllIcons.General.Error | ||
val Remove = AllIcons.Diff.Remove | ||
|
||
fun Icon.scaledToText(parentComponent: Component? = null): Icon { | ||
val settingsManager: NotRoamableUiSettings = NotRoamableUiSettings.getInstance() | ||
val settings = settingsManager.state | ||
return IconUtil.scaleByFont(this, parentComponent, settings.fontSize) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/jetbrains-plugin/src/main/kotlin/com/mongodb/jbplugin/i18n/MdbToolbarMessages.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mongodb.jbplugin.i18n | ||
|
||
import com.intellij.DynamicBundle | ||
import org.jetbrains.annotations.Nls | ||
import org.jetbrains.annotations.NonNls | ||
import org.jetbrains.annotations.PropertyKey | ||
|
||
object MdbToolbarMessages { | ||
@NonNls | ||
private const val BUNDLE = "messages.MdbToolbarBundle" | ||
private val instance = DynamicBundle(MdbToolbarMessages::class.java, BUNDLE) | ||
|
||
fun message( | ||
key: | ||
@PropertyKey(resourceBundle = BUNDLE) | ||
String, | ||
vararg params: Any, | ||
): @Nls String = instance.getMessage(key, *params) | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/jetbrains-plugin/src/main/resources/icons/GreenCircle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions
2
packages/jetbrains-plugin/src/main/resources/messages/MdbToolbarBundle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
attach.datasource.to.editor=Attach MongoDB data source | ||
detach.datasource.from.editor=Detach data source |
Oops, something went wrong.