Use correct object
[LibreOffice.git] / cui / source / options / optjava.cxx
blob4a4ea15ff695ad8802fdef842d9411f0c5ce53bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <o3tl/safeint.hxx>
21 #include <sal/config.h>
22 #include <sal/log.hxx>
24 #include <cassert>
25 #include <memory>
27 #include <config_java.h>
29 #include "optaboutconfig.hxx"
30 #include "optjava.hxx"
31 #include <treeopt.hxx>
32 #include <dialmgr.hxx>
34 #include <officecfg/Office/Common.hxx>
35 #include <osl/file.hxx>
36 #include <rtl/bootstrap.hxx>
38 #include <strings.hrc>
39 #include <vcl/svapp.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/urlobj.hxx>
42 #include <vcl/weld.hxx>
43 #include <unotools/pathoptions.hxx>
44 #include <svtools/imagemgr.hxx>
45 #include <svtools/restartdialog.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/inputdlg.hxx>
48 #include <comphelper/diagnose_ex.hxx>
49 #include <comphelper/processfactory.hxx>
50 #include <comphelper/string.hxx>
51 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
52 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
53 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
54 #if HAVE_FEATURE_JAVA
55 #include <jvmfwk/framework.hxx>
56 #endif
58 // define ----------------------------------------------------------------
60 #define CLASSPATH_DELIMITER SAL_PATHSEPARATOR
62 using namespace ::com::sun::star::ui::dialogs;
63 using namespace ::com::sun::star::uno;
65 // class SvxJavaOptionsPage ----------------------------------------------
66 SvxJavaOptionsPage::SvxJavaOptionsPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
67 : SfxTabPage(pPage, pController, u"cui/ui/optadvancedpage.ui"_ustr, u"OptAdvancedPage"_ustr, &rSet)
68 , m_aResetIdle("cui options SvxJavaOptionsPage Reset")
69 , xDialogListener(new ::svt::DialogClosedListener())
70 , m_xJavaEnableCB(m_xBuilder->weld_check_button(u"javaenabled"_ustr))
71 , m_xJavaList(m_xBuilder->weld_tree_view(u"javas"_ustr))
72 , m_xJavaPathText(m_xBuilder->weld_label(u"javapath"_ustr))
73 , m_xAddBtn(m_xBuilder->weld_button(u"add"_ustr))
74 , m_xParameterBtn(m_xBuilder->weld_button(u"parameters"_ustr))
75 , m_xClassPathBtn(m_xBuilder->weld_button(u"classpath"_ustr))
76 , m_xExpertConfigBtn(m_xBuilder->weld_button(u"expertconfig"_ustr))
77 , m_xExperimentalCB(m_xBuilder->weld_check_button(u"experimental"_ustr))
78 , m_xExperimentalImg(m_xBuilder->weld_widget(u"lockexperimental"_ustr))
79 , m_xMacroCB(m_xBuilder->weld_check_button(u"macrorecording"_ustr))
80 , m_xMacroImg(m_xBuilder->weld_widget(u"lockmacrorecording"_ustr))
81 , m_xAddDialogText(m_xBuilder->weld_label(u"selectruntime"_ustr))
82 , m_xJavaFrame(m_xBuilder->weld_widget(u"javaframe"_ustr))
84 m_sInstallText = m_xJavaPathText->get_label();
85 m_sAddDialogText = m_xAddDialogText->get_label();
87 m_xJavaList->set_size_request(m_xJavaList->get_approximate_digit_width() * 30,
88 m_xJavaList->get_height_rows(8));
90 m_xJavaList->enable_toggle_buttons(weld::ColumnToggleType::Radio);
91 m_xJavaList->connect_toggled( LINK( this, SvxJavaOptionsPage, CheckHdl_Impl ) );
92 m_xJavaList->connect_selection_changed(LINK(this, SvxJavaOptionsPage, SelectHdl_Impl));
94 std::vector<int> aWidths
96 m_xJavaList->get_checkbox_column_width(),
97 o3tl::narrowing<int>(m_xJavaList->get_pixel_size(u"Sun Microsystems Inc."_ustr).Width())
99 m_xJavaList->set_column_fixed_widths(aWidths);
101 m_xJavaEnableCB->connect_toggled( LINK( this, SvxJavaOptionsPage, EnableHdl_Impl ) );
102 m_xAddBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, AddHdl_Impl ) );
103 m_xParameterBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ParameterHdl_Impl ) );
104 m_xClassPathBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ClassPathHdl_Impl ) );
105 m_aResetIdle.SetInvokeHandler( LINK( this, SvxJavaOptionsPage, ResetHdl_Impl ) );
107 m_xExpertConfigBtn->connect_clicked( LINK( this, SvxJavaOptionsPage, ExpertConfigHdl_Impl) );
108 if (!officecfg::Office::Common::Security::EnableExpertConfiguration::get())
109 m_xExpertConfigBtn->set_sensitive(false);
111 if (officecfg::Office::Common::Misc::MacroRecorderMode::isReadOnly())
113 m_xMacroCB->set_sensitive(false);
114 m_xMacroImg->set_visible(true);
117 if (officecfg::Office::Common::Misc::ExperimentalMode::isReadOnly())
119 m_xExperimentalCB->set_sensitive(false);
120 m_xExperimentalImg->set_visible(true);
123 xDialogListener->SetDialogClosedLink( LINK( this, SvxJavaOptionsPage, DialogClosedHdl ) );
125 EnableHdl_Impl(*m_xJavaEnableCB);
126 #if HAVE_FEATURE_JAVA
127 jfw_lock();
128 #else
129 m_xJavaFrame->set_sensitive(false);
130 #endif
133 SvxJavaOptionsPage::~SvxJavaOptionsPage()
135 ClearJavaInfo();
136 #if HAVE_FEATURE_JAVA
137 m_aAddedInfos.clear();
139 jfw_unlock();
140 #endif
143 IMPL_LINK_NOARG(SvxJavaOptionsPage, EnableHdl_Impl, weld::Toggleable&, void)
145 bool bEnable = m_xJavaFrame->get_sensitive() && m_xJavaEnableCB->get_active();
146 m_xJavaList->set_sensitive(bEnable);
149 IMPL_LINK(SvxJavaOptionsPage, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
151 HandleCheckEntry(m_xJavaList->get_iter_index_in_parent(rRowCol.first));
154 IMPL_LINK_NOARG(SvxJavaOptionsPage, SelectHdl_Impl, weld::TreeView&, void)
156 UpdateJavaPathText();
159 IMPL_LINK_NOARG(SvxJavaOptionsPage, AddHdl_Impl, weld::Button&, void)
163 const Reference < XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
164 xFolderPicker = sfx2::createFolderPicker(xContext, GetFrameWeld());
166 OUString sWorkFolder = SvtPathOptions().GetWorkPath();
167 xFolderPicker->setDisplayDirectory( sWorkFolder );
168 xFolderPicker->setDescription( m_sAddDialogText );
170 Reference< XAsynchronousExecutableDialog > xAsyncDlg( xFolderPicker, UNO_QUERY );
171 if ( xAsyncDlg.is() )
172 xAsyncDlg->startExecuteModal( xDialogListener );
173 else if ( xFolderPicker.is() && xFolderPicker->execute() == ExecutableDialogResults::OK )
174 AddFolder( xFolderPicker->getDirectory() );
176 catch (const Exception&)
178 TOOLS_WARN_EXCEPTION( "cui.options", "SvxJavaOptionsPage::AddHdl_Impl()");
182 IMPL_LINK_NOARG(SvxJavaOptionsPage, ParameterHdl_Impl, weld::Button&, void)
184 #if HAVE_FEATURE_JAVA
185 std::vector< OUString > aParameterList;
186 if (!m_xParamDlg)
188 m_xParamDlg.reset(new SvxJavaParameterDlg(GetFrameWeld()));
189 javaFrameworkError eErr = jfw_getVMParameters( &m_parParameters );
190 if ( JFW_E_NONE == eErr && !m_parParameters.empty() )
192 aParameterList = m_parParameters;
193 m_xParamDlg->SetParameters( aParameterList );
196 else
198 aParameterList = m_xParamDlg->GetParameters();
199 m_xParamDlg->DisableButtons(); //disable add, edit and remove button when dialog is reopened
202 if (m_xParamDlg->run() == RET_OK)
204 if ( aParameterList != m_xParamDlg->GetParameters() )
206 aParameterList = m_xParamDlg->GetParameters();
207 if ( jfw_isVMRunning() )
209 RequestRestart( svtools::RESTART_REASON_ASSIGNING_JAVAPARAMETERS );
213 else
214 m_xParamDlg->SetParameters( aParameterList );
215 #else
216 (void) this; // Silence loplugin:staticmethods
217 #endif
221 IMPL_LINK_NOARG(SvxJavaOptionsPage, ClassPathHdl_Impl, weld::Button&, void)
223 #if HAVE_FEATURE_JAVA
224 OUString sClassPath;
226 if ( !m_xPathDlg )
228 m_xPathDlg.reset(new SvxJavaClassPathDlg(GetFrameWeld()));
229 javaFrameworkError eErr = jfw_getUserClassPath( &m_pClassPath );
230 if ( JFW_E_NONE == eErr )
232 sClassPath = m_pClassPath;
233 m_xPathDlg->SetClassPath( sClassPath );
236 else
237 sClassPath = m_xPathDlg->GetClassPath();
239 m_xPathDlg->SetFocus();
240 if (m_xPathDlg->run() == RET_OK)
243 if (m_xPathDlg->GetClassPath() != sClassPath)
245 sClassPath = m_xPathDlg->GetClassPath();
246 if ( jfw_isVMRunning() )
248 RequestRestart( svtools::RESTART_REASON_ASSIGNING_FOLDERS );
252 else
253 m_xPathDlg->SetClassPath( sClassPath );
254 #else
255 (void) this;
256 #endif
260 IMPL_LINK_NOARG(SvxJavaOptionsPage, ResetHdl_Impl, Timer *, void)
262 LoadJREs();
266 IMPL_LINK_NOARG(SvxJavaOptionsPage, StartFolderPickerHdl, void*, void)
270 Reference< XAsynchronousExecutableDialog > xAsyncDlg( xFolderPicker, UNO_QUERY );
271 if ( xAsyncDlg.is() )
272 xAsyncDlg->startExecuteModal( xDialogListener );
273 else if ( xFolderPicker.is() && xFolderPicker->execute() == ExecutableDialogResults::OK )
274 AddFolder( xFolderPicker->getDirectory() );
276 catch ( Exception const & )
278 TOOLS_WARN_EXCEPTION( "cui.options", "SvxJavaOptionsPage::StartFolderPickerHdl()" );
282 IMPL_LINK( SvxJavaOptionsPage, DialogClosedHdl, DialogClosedEvent*, pEvt, void )
284 if ( RET_OK == pEvt->DialogResult )
286 DBG_ASSERT( xFolderPicker.is(), "SvxJavaOptionsPage::DialogClosedHdl(): no folder picker" );
288 AddFolder( xFolderPicker->getDirectory() );
292 IMPL_LINK_NOARG(SvxJavaOptionsPage, ExpertConfigHdl_Impl, weld::Button&, void)
294 CuiAboutConfigTabPage aExpertConfigDlg(GetFrameWeld());
296 weld::WaitObject aWait(GetFrameWeld());
297 aExpertConfigDlg.Reset();//initialize and reset function
300 if (RET_OK == aExpertConfigDlg.run())
302 aExpertConfigDlg.FillItemSet();//save changes if there are any
306 void SvxJavaOptionsPage::ClearJavaInfo()
308 #if HAVE_FEATURE_JAVA
309 m_parJavaInfo.clear();
310 #else
311 (void) this;
312 #endif
315 void SvxJavaOptionsPage::LoadJREs()
317 #if HAVE_FEATURE_JAVA
318 weld::WaitObject aWaitObj(GetFrameWeld());
319 javaFrameworkError eErr = jfw_findAllJREs( &m_parJavaInfo );
320 if ( JFW_E_NONE == eErr )
322 for (auto const & pInfo: m_parJavaInfo)
324 AddJRE( pInfo.get() );
328 for (auto const & pInfo: m_aAddedInfos)
330 AddJRE( pInfo.get() );
333 std::unique_ptr<JavaInfo> pSelectedJava;
334 eErr = jfw_getSelectedJRE( &pSelectedJava );
335 if ( !(JFW_E_NONE == eErr && pSelectedJava) )
336 return;
338 sal_Int32 i = 0;
339 for (auto const & pCmpInfo: m_parJavaInfo)
341 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pSelectedJava.get() ) )
343 HandleCheckEntry(i);
344 UpdateJavaPathText();
345 break;
347 ++i;
349 #else
350 (void) this;
351 #endif
355 void SvxJavaOptionsPage::AddJRE( JavaInfo const * _pInfo )
357 #if HAVE_FEATURE_JAVA
358 int nPos = m_xJavaList->n_children();
359 m_xJavaList->append();
360 m_xJavaList->set_toggle(nPos, TRISTATE_FALSE);
361 m_xJavaList->set_text(nPos, _pInfo->sVendor, 1);
362 m_xJavaList->set_text(nPos, _pInfo->sVersion, 2);
364 INetURLObject aLocObj(_pInfo->sLocation);
365 OUString sLocation = aLocObj.getFSysPath(FSysStyle::Detect);
366 m_xJavaList->set_id(nPos, sLocation);
367 #else
368 (void) this;
369 (void)_pInfo;
370 #endif
373 void SvxJavaOptionsPage::HandleCheckEntry(int nCheckedRow)
375 m_xJavaList->select(nCheckedRow);
376 for (int i = 0, nCount = m_xJavaList->n_children(); i < nCount; ++i)
378 // we have radio button behavior -> so uncheck the other entries
379 m_xJavaList->set_toggle(i, i == nCheckedRow ? TRISTATE_TRUE : TRISTATE_FALSE);
383 void SvxJavaOptionsPage::UpdateJavaPathText()
385 assert(m_xJavaList->get_selected_index() != -1);
386 // set installation directory info
387 OUString sLocation = m_xJavaList->get_selected_id();
388 // tdf#80646 insert LTR mark after label
389 OUString sInfo = m_sInstallText + u"\u200E" + sLocation;
390 m_xJavaPathText->set_label(sInfo);
393 void SvxJavaOptionsPage::AddFolder( const OUString& _rFolder )
395 #if HAVE_FEATURE_JAVA
396 bool bStartAgain = true;
397 std::unique_ptr<JavaInfo> pInfo;
398 javaFrameworkError eErr = jfw_getJavaInfoByPath( _rFolder, &pInfo );
399 if ( JFW_E_NONE == eErr && pInfo )
401 sal_Int32 nPos = 0;
402 bool bFound = false;
403 for (auto const & pCmpInfo: m_parJavaInfo)
405 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pInfo.get() ) )
407 bFound = true;
408 break;
410 ++nPos;
413 if ( !bFound )
415 for (auto const & pCmpInfo: m_aAddedInfos)
417 if ( jfw_areEqualJavaInfo( pCmpInfo.get(), pInfo.get() ) )
419 bFound = true;
420 break;
422 ++nPos;
426 if ( !bFound )
428 jfw_addJRELocation( pInfo->sLocation );
429 AddJRE( pInfo.get() );
430 m_aAddedInfos.push_back( std::move(pInfo) );
431 nPos = m_xJavaList->n_children() - 1;
434 HandleCheckEntry(nPos);
435 UpdateJavaPathText();
436 bStartAgain = false;
438 else if ( JFW_E_NOT_RECOGNIZED == eErr )
440 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
441 VclMessageType::Warning, VclButtonsType::Ok,
442 CuiResId(RID_CUISTR_JRE_NOT_RECOGNIZED)));
443 xBox->run();
445 else if ( JFW_E_FAILED_VERSION == eErr )
447 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetFrameWeld(),
448 VclMessageType::Warning, VclButtonsType::Ok,
449 CuiResId(RID_CUISTR_JRE_FAILED_VERSION)));
450 xBox->run();
453 if ( bStartAgain )
455 xFolderPicker->setDisplayDirectory( _rFolder );
456 Application::PostUserEvent( LINK( this, SvxJavaOptionsPage, StartFolderPickerHdl ) );
458 #else
459 (void) this;
460 (void)_rFolder;
461 #endif
464 void SvxJavaOptionsPage::RequestRestart(svtools::RestartReason eReason)
466 OfaTreeOptionsDialog* pParentDlg(static_cast<OfaTreeOptionsDialog*>(GetDialogController()));
467 if (pParentDlg)
468 pParentDlg->SetNeedsRestart(eReason);
471 std::unique_ptr<SfxTabPage> SvxJavaOptionsPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
473 return std::make_unique<SvxJavaOptionsPage>(pPage, pController, *rAttrSet);
476 OUString SvxJavaOptionsPage::GetAllStrings()
478 OUString sAllStrings;
479 OUString labels[] = { u"label1"_ustr, u"label2"_ustr, u"javapath"_ustr, u"selectruntime"_ustr, u"label12"_ustr };
481 for (const auto& label : labels)
483 if (const auto pString = m_xBuilder->weld_label(label))
484 sAllStrings += pString->get_label() + " ";
487 OUString checkButton[] = { u"javaenabled"_ustr, u"experimental"_ustr, u"macrorecording"_ustr };
489 for (const auto& check : checkButton)
491 if (const auto pString = m_xBuilder->weld_check_button(check))
492 sAllStrings += pString->get_label() + " ";
495 OUString buttons[] = { u"add"_ustr, u"parameters"_ustr, u"classpath"_ustr, u"expertconfig"_ustr };
497 for (const auto& btn : buttons)
499 if (const auto pString = m_xBuilder->weld_button(btn))
500 sAllStrings += pString->get_label() + " ";
503 return sAllStrings.replaceAll("_", "");
506 bool SvxJavaOptionsPage::FillItemSet( SfxItemSet* /*rCoreSet*/ )
508 bool bModified = false;
510 if ( m_xExperimentalCB->get_state_changed_from_saved() )
512 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
513 comphelper::ConfigurationChanges::create());
514 officecfg::Office::Common::Misc::ExperimentalMode::set( m_xExperimentalCB->get_active(), xChanges );
515 xChanges->commit();
516 bModified = true;
517 RequestRestart( svtools::RESTART_REASON_EXP_FEATURES );
520 if ( m_xMacroCB->get_state_changed_from_saved() )
522 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
523 comphelper::ConfigurationChanges::create());
524 officecfg::Office::Common::Misc::MacroRecorderMode::set( m_xMacroCB->get_active(), xChanges );
525 xChanges->commit();
526 bModified = true;
529 #if HAVE_FEATURE_JAVA
530 javaFrameworkError eErr = JFW_E_NONE;
531 if (m_xParamDlg)
533 eErr = jfw_setVMParameters(m_xParamDlg->GetParameters());
534 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setVMParameters");
535 bModified = true;
538 if (m_xPathDlg)
540 OUString sPath(m_xPathDlg->GetClassPath());
541 if (m_xPathDlg->GetOldPath() != sPath)
543 eErr = jfw_setUserClassPath( sPath );
544 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setUserClassPath");
545 bModified = true;
549 sal_uInt32 nCount = m_xJavaList->n_children();
550 for (sal_uInt32 i = 0; i < nCount; ++i)
552 if (m_xJavaList->get_toggle(i) == TRISTATE_TRUE)
554 JavaInfo const * pInfo;
555 if ( i < m_parJavaInfo.size() )
556 pInfo = m_parJavaInfo[i].get();
557 else
558 pInfo = m_aAddedInfos[ i - m_parJavaInfo.size() ].get();
560 std::unique_ptr<JavaInfo> pSelectedJava;
561 eErr = jfw_getSelectedJRE( &pSelectedJava );
562 if ( JFW_E_NONE == eErr || JFW_E_INVALID_SETTINGS == eErr )
564 if (!pSelectedJava || !jfw_areEqualJavaInfo( pInfo, pSelectedJava.get() ) )
566 if ( jfw_isVMRunning() ||
567 ( ( pInfo->nRequirements & JFW_REQUIRE_NEEDRESTART ) == JFW_REQUIRE_NEEDRESTART ) )
569 RequestRestart( svtools::RESTART_REASON_JAVA );
572 eErr = jfw_setSelectedJRE( pInfo );
573 SAL_WARN_IF(JFW_E_NONE != eErr, "cui.options", "SvxJavaOptionsPage::FillItemSet(): error in jfw_setSelectedJRE");
574 bModified = true;
577 break;
581 bool bEnabled = false;
582 eErr = jfw_getEnabled( &bEnabled );
583 DBG_ASSERT( JFW_E_NONE == eErr,
584 "SvxJavaOptionsPage::FillItemSet(): error in jfw_getEnabled" );
585 if ( bEnabled != m_xJavaEnableCB->get_active() )
587 eErr = jfw_setEnabled( m_xJavaEnableCB->get_active() );
588 DBG_ASSERT( JFW_E_NONE == eErr,
589 "SvxJavaOptionsPage::FillItemSet(): error in jfw_setEnabled" );
590 bModified = true;
592 #endif
594 return bModified;
598 void SvxJavaOptionsPage::Reset( const SfxItemSet* /*rSet*/ )
600 ClearJavaInfo();
601 m_xJavaList->clear();
603 #if HAVE_FEATURE_JAVA
604 bool bEnabled = false;
605 javaFrameworkError eErr = jfw_getEnabled( &bEnabled );
606 if (eErr == JFW_E_DIRECT_MODE)
608 // direct mode disregards Java settings made here, so gray them out
609 m_xJavaFrame->set_sensitive(false);
610 // check whether a usable JRE was set
611 std::unique_ptr<JavaInfo> pSelectedJava;
612 eErr = jfw_getSelectedJRE( &pSelectedJava );
613 bEnabled = (eErr == JFW_E_NONE);
615 else if ( eErr != JFW_E_NONE )
616 bEnabled = false;
617 m_xJavaEnableCB->set_active(bEnabled);
618 EnableHdl_Impl(*m_xJavaEnableCB);
619 #else
620 m_xJavaEnableCB->set_active(false);
621 m_xJavaEnableCB->set_sensitive(false);
622 #endif
624 m_xExperimentalCB->set_active( officecfg::Office::Common::Misc::ExperimentalMode::get() );
625 m_xExperimentalCB->save_state();
626 m_xMacroCB->set_active( officecfg::Office::Common::Misc::MacroRecorderMode::get() );
627 m_xMacroCB->save_state();
629 m_aResetIdle.Start();
633 void SvxJavaOptionsPage::FillUserData()
635 SetUserData( OUString() );
638 // class SvxJavaParameterDlg ---------------------------------------------
640 SvxJavaParameterDlg::SvxJavaParameterDlg(weld::Window* pParent)
641 : GenericDialogController(pParent, u"cui/ui/javastartparametersdialog.ui"_ustr,
642 u"JavaStartParameters"_ustr)
643 , m_xParameterEdit(m_xBuilder->weld_entry(u"parameterfield"_ustr))
644 , m_xAssignBtn(m_xBuilder->weld_button(u"assignbtn"_ustr))
645 , m_xAssignedList(m_xBuilder->weld_tree_view(u"assignlist"_ustr))
646 , m_xRemoveBtn(m_xBuilder->weld_button(u"removebtn"_ustr))
647 , m_xEditBtn(m_xBuilder->weld_button(u"editbtn"_ustr))
649 m_xAssignedList->set_size_request(m_xAssignedList->get_approximate_digit_width() * 54,
650 m_xAssignedList->get_height_rows(6));
651 m_xParameterEdit->connect_changed( LINK( this, SvxJavaParameterDlg, ModifyHdl_Impl ) );
652 m_xParameterEdit->connect_activate(LINK(this, SvxJavaParameterDlg, ActivateHdl_Impl));
653 m_xAssignBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, AssignHdl_Impl ) );
654 m_xRemoveBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, RemoveHdl_Impl ) );
655 m_xEditBtn->connect_clicked( LINK( this, SvxJavaParameterDlg, EditHdl_Impl ) );
656 m_xAssignedList->connect_selection_changed(LINK(this, SvxJavaParameterDlg, SelectHdl_Impl));
657 m_xAssignedList->connect_row_activated( LINK( this, SvxJavaParameterDlg, DblClickHdl_Impl ) );
659 ModifyHdl_Impl(*m_xParameterEdit);
660 EnableEditButton();
661 EnableRemoveButton();
664 SvxJavaParameterDlg::~SvxJavaParameterDlg()
668 IMPL_LINK_NOARG(SvxJavaParameterDlg, ModifyHdl_Impl, weld::Entry&, void)
670 OUString sParam = comphelper::string::strip(m_xParameterEdit->get_text(), ' ');
671 m_xAssignBtn->set_sensitive(!sParam.isEmpty());
674 IMPL_LINK_NOARG(SvxJavaParameterDlg, AssignHdl_Impl, weld::Button&, void)
676 OUString sParam = comphelper::string::strip(m_xParameterEdit->get_text(), ' ');
677 if (sParam.isEmpty())
678 return;
680 int nPos = m_xAssignedList->find_text(sParam);
681 if (nPos == -1)
683 m_xAssignedList->append_text(sParam);
684 m_xAssignedList->select(m_xAssignedList->n_children() - 1);
686 else
687 m_xAssignedList->select(nPos);
688 m_xParameterEdit->set_text(OUString());
689 ModifyHdl_Impl(*m_xParameterEdit);
690 EnableEditButton();
691 EnableRemoveButton();
694 IMPL_LINK_NOARG(SvxJavaParameterDlg, ActivateHdl_Impl, weld::Entry&, bool)
696 if (m_xAssignBtn->get_sensitive())
698 AssignHdl_Impl(*m_xAssignBtn);
699 return true;
701 return false;
704 IMPL_LINK_NOARG(SvxJavaParameterDlg, EditHdl_Impl, weld::Button&, void)
706 EditParameter();
709 IMPL_LINK_NOARG(SvxJavaParameterDlg, SelectHdl_Impl, weld::TreeView&, void)
711 EnableEditButton();
712 EnableRemoveButton();
715 IMPL_LINK_NOARG(SvxJavaParameterDlg, DblClickHdl_Impl, weld::TreeView&, bool)
717 EditParameter();
718 return true;
721 IMPL_LINK_NOARG(SvxJavaParameterDlg, RemoveHdl_Impl, weld::Button&, void)
723 int nPos = m_xAssignedList->get_selected_index();
724 if (nPos != -1)
726 m_xAssignedList->remove(nPos);
727 int nCount = m_xAssignedList->n_children();
728 if (nCount)
730 if (nPos >= nCount)
731 nPos = nCount - 1;
732 m_xAssignedList->select(nPos);
734 else
736 DisableEditButton();
739 EnableRemoveButton();
742 void SvxJavaParameterDlg::EditParameter()
744 int nPos = m_xAssignedList->get_selected_index();
745 m_xParameterEdit->set_text(OUString());
747 if (nPos == -1)
748 return;
750 InputDialog aParamEditDlg(m_xDialog.get(), CuiResId(RID_CUISTR_JAVA_START_PARAM));
751 OUString editableClassPath = m_xAssignedList->get_selected_text();
752 aParamEditDlg.SetEntryText(editableClassPath);
753 aParamEditDlg.HideHelpBtn();
755 if (!aParamEditDlg.run())
756 return;
757 OUString editedClassPath = comphelper::string::strip(aParamEditDlg.GetEntryText(), ' ');
759 if ( !editedClassPath.isEmpty() && editableClassPath != editedClassPath )
761 m_xAssignedList->remove(nPos);
762 m_xAssignedList->insert_text(nPos, editedClassPath);
763 m_xAssignedList->select(nPos);
767 short SvxJavaParameterDlg::run()
769 m_xParameterEdit->grab_focus();
770 m_xAssignedList->select(-1);
771 return GenericDialogController::run();
774 std::vector< OUString > SvxJavaParameterDlg::GetParameters() const
776 int nCount = m_xAssignedList->n_children();
777 std::vector< OUString > aParamList;
778 aParamList.reserve(nCount);
779 for (int i = 0; i < nCount; ++i)
780 aParamList.push_back(m_xAssignedList->get_text(i));
781 return aParamList;
784 void SvxJavaParameterDlg::DisableButtons()
786 DisableAssignButton();
787 DisableEditButton();
788 DisableRemoveButton();
791 void SvxJavaParameterDlg::SetParameters( std::vector< OUString > const & rParams )
793 m_xAssignedList->clear();
794 for (auto const & sParam: rParams)
796 m_xAssignedList->append_text(sParam);
798 DisableEditButton();
799 DisableRemoveButton();
803 // class SvxJavaClassPathDlg ---------------------------------------------
805 SvxJavaClassPathDlg::SvxJavaClassPathDlg(weld::Window* pParent)
806 : GenericDialogController(pParent, u"cui/ui/javaclasspathdialog.ui"_ustr, u"JavaClassPath"_ustr)
807 , m_xPathList(m_xBuilder->weld_tree_view(u"paths"_ustr))
808 , m_xAddArchiveBtn(m_xBuilder->weld_button(u"archive"_ustr))
809 , m_xAddPathBtn(m_xBuilder->weld_button(u"folder"_ustr))
810 , m_xRemoveBtn(m_xBuilder->weld_button(u"remove"_ustr))
812 m_xPathList->set_size_request(m_xPathList->get_approximate_digit_width() * 54,
813 m_xPathList->get_height_rows(8));
814 m_xAddArchiveBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, AddArchiveHdl_Impl ) );
815 m_xAddPathBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, AddPathHdl_Impl ) );
816 m_xRemoveBtn->connect_clicked( LINK( this, SvxJavaClassPathDlg, RemoveHdl_Impl ) );
817 m_xPathList->connect_selection_changed(LINK(this, SvxJavaClassPathDlg, SelectHdl_Impl));
819 // set initial focus to path list
820 m_xPathList->grab_focus();
823 SvxJavaClassPathDlg::~SvxJavaClassPathDlg()
827 IMPL_LINK_NOARG(SvxJavaClassPathDlg, AddArchiveHdl_Impl, weld::Button&, void)
829 sfx2::FileDialogHelper aDlg(TemplateDescription::FILEOPEN_SIMPLE, FileDialogFlags::NONE, m_xDialog.get());
830 aDlg.SetTitle( CuiResId( RID_CUISTR_ARCHIVE_TITLE ) );
831 aDlg.AddFilter( CuiResId( RID_CUISTR_ARCHIVE_HEADLINE ), u"*.jar;*.zip"_ustr );
832 aDlg.SetContext(sfx2::FileDialogHelper::JavaClassPath);
833 OUString sFolder;
834 if (m_xPathList->count_selected_rows() > 0)
836 osl::FileBase::getFileURLFromSystemPath(m_xPathList->get_selected_text(), sFolder);
837 // best effort
839 if (!sFolder.isEmpty())
840 aDlg.SetDisplayDirectory( sFolder );
841 if ( aDlg.Execute() == ERRCODE_NONE )
843 OUString sURL = aDlg.GetPath();
844 OUString sFile;
845 if (osl::FileBase::getSystemPathFromFileURL(sURL, sFile) == osl::FileBase::E_None)
847 INetURLObject aURL( sURL );
848 if ( !IsPathDuplicate( sFile ) )
850 m_xPathList->append(u""_ustr, sFile, SvFileInformationManager::GetImageId(aURL));
851 m_xPathList->select(m_xPathList->n_children() - 1);
853 else
855 OUString sMsg( CuiResId( RID_CUISTR_MULTIFILE_DBL_ERR ) );
856 sMsg = sMsg.replaceFirst( "%1", sFile );
857 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
858 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
859 xBox->run();
862 else
864 OUString sMsg( CuiResId( RID_CUISTR_CANNOTCONVERTURL_ERR ) );
865 sMsg = sMsg.replaceFirst( "%1", sURL );
866 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
867 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
868 xBox->run();
871 EnableRemoveButton();
874 IMPL_LINK_NOARG(SvxJavaClassPathDlg, AddPathHdl_Impl, weld::Button&, void)
876 const Reference < XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
877 Reference < XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_xDialog.get());
879 OUString sOldFolder;
880 if (m_xPathList->count_selected_rows() > 0)
882 osl::FileBase::getFileURLFromSystemPath(m_xPathList->get_selected_text(), sOldFolder);
883 // best effort
885 if (sOldFolder.isEmpty())
886 sOldFolder = SvtPathOptions().GetWorkPath();
887 xFolderPicker->setDisplayDirectory( sOldFolder );
888 if ( xFolderPicker->execute() == ExecutableDialogResults::OK )
890 OUString sFolderURL( xFolderPicker->getDirectory() );
891 INetURLObject aURL( sFolderURL );
892 OUString sNewFolder;
893 if (osl::FileBase::getSystemPathFromFileURL(sFolderURL, sNewFolder)
894 == osl::FileBase::E_None)
896 if ( !IsPathDuplicate( sNewFolder ) )
898 m_xPathList->append(u""_ustr, sNewFolder, SvFileInformationManager::GetImageId(aURL));
899 m_xPathList->select(m_xPathList->n_children() - 1);
901 else
903 OUString sMsg( CuiResId( RID_CUISTR_MULTIFILE_DBL_ERR ) );
904 sMsg = sMsg.replaceFirst( "%1", sNewFolder );
905 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
906 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
907 xBox->run();
910 else
912 OUString sMsg( CuiResId( RID_CUISTR_CANNOTCONVERTURL_ERR ) );
913 sMsg = sMsg.replaceFirst( "%1", sFolderURL );
914 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
915 VclMessageType::Warning, VclButtonsType::Ok, sMsg));
916 xBox->run();
919 EnableRemoveButton();
922 IMPL_LINK_NOARG(SvxJavaClassPathDlg, RemoveHdl_Impl, weld::Button&, void)
924 int nPos = m_xPathList->get_selected_index();
925 if (nPos != -1)
927 m_xPathList->remove(nPos);
928 int nCount = m_xPathList->n_children();
929 if (nCount)
931 if (nPos >= nCount)
932 nPos = nCount - 1;
933 m_xPathList->select( nPos );
937 EnableRemoveButton();
940 IMPL_LINK_NOARG(SvxJavaClassPathDlg, SelectHdl_Impl, weld::TreeView&, void)
942 EnableRemoveButton();
945 bool SvxJavaClassPathDlg::IsPathDuplicate( std::u16string_view _rPath )
947 bool bRet = false;
948 int nCount = m_xPathList->n_children();
949 for (int i = 0; i < nCount; ++i)
951 if ( m_xPathList->get_text(i) == _rPath )
953 bRet = true;
954 break;
958 return bRet;
961 OUString SvxJavaClassPathDlg::GetClassPath() const
963 OUStringBuffer sPath;
964 int nCount = m_xPathList->n_children();
965 for (int i = 0; i < nCount; ++i)
967 if (!sPath.isEmpty())
968 sPath.append(CLASSPATH_DELIMITER);
969 sPath.append(m_xPathList->get_text(i));
971 return sPath.makeStringAndClear();
974 #if HAVE_FEATURE_JAVA
975 void SvxJavaClassPathDlg::SetClassPath( const OUString& _rPath )
977 if ( m_sOldPath.isEmpty() )
978 m_sOldPath = _rPath;
979 m_xPathList->clear();
980 if (!_rPath.isEmpty())
982 std::vector paths = jfw_convertUserPathList(_rPath);
983 for (auto const& path : paths)
985 OUString sURL;
986 if (path.startsWith("$"))
988 sURL = path;
989 rtl::Bootstrap::expandMacros(sURL);
991 else
993 osl::FileBase::getFileURLFromSystemPath(path, sURL);
995 INetURLObject aURL( sURL );
996 m_xPathList->append(u""_ustr, path, SvFileInformationManager::GetImageId(aURL));
998 // select first entry
999 m_xPathList->select(0);
1001 SelectHdl_Impl(*m_xPathList);
1003 #endif
1005 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */